php - 表单中下拉框的数据作为 'Array' 发送到数据库

标签 php mysql forms drop-down-menu

我一直在尝试制作一个带有下拉列表的表单,这些下拉列表由不同的表填充,然后将数据上传到另一个表中。

我拼凑了以下代码,但在提交表单时,表中的数据显示为“数组”,而不是下拉列表中的所选项目,最后的“数量”提交正常。

<?php
include ('../connect.php');

if(isset($_POST['submit']))
{

$qty = $_POST['qty'];
$mob_name = $_POST['mob_name'];
$item_img = $_POST['item_img'];
$item_name = $_POST['item_name'];


//save the name of image in table
$query = mysql_query("INSERT INTO tbl_drop VALUES('','$mob_name','$item_img','$item_name','$qty')") or die(mysql_error());


}

$query_Recordset1 = "SELECT mob_name FROM tbl_img ORDER BY mob_name ASC";
$Recordset1 = mysql_query($query_Recordset1) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$query_Recordset2 = "SELECT item_name FROM tbl_itm ORDER BY item_name ASC";
$Recordset2 = mysql_query($query_Recordset2) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

$query_Recordset3 = "SELECT item_img FROM tbl_itm ORDER BY item_name ASC";
$Recordset3 = mysql_query($query_Recordset3) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

?>

<html>
<head>
<title></title>
</head>

<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="new_drop.php">
<label>Mob Name:
<select name="mob_name" id="mob_name">
<?php
do {
?>
<option value="<?php echo $row_Recordset1?>"><?php echo $row_Recordset1['mob_name']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select>
</label>
<p>
<label>Item Name:
<select name="item_name" id="item_name">
<?php
do {
?>
<option value="<?php echo $row_Recordset2?>"><?php echo $row_Recordset2['item_name']?></option>
<?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
$rows = mysql_num_rows($Recordset2);
if($rows > 0) {
mysql_data_seek($Recordset2, 0);
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
}
?>
</select>
</label>
<p>
<label>Item Image:
<select name="item_img" id="item_img">
<?php
do {
?>
<option value="<?php echo $row_Recordset3?>"><?php echo $row_Recordset3['item_img']?></option>
<?php
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
$rows = mysql_num_rows($Recordset3);
if($rows > 0) {
mysql_data_seek($Recordset3, 0);
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
}
?>
</select>
</label>
<p>
<label>Quantity:
<input type='text' name='qty' />
</label>
<p>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset2);
mysql_free_result($Recordset3);
?>

最佳答案

您的代码中有一个错误:

<option value="<?php echo $row_Recordset3?>"><?php echo $row_Recordset3['item_img']?></option>

应该是

<option value="<?php echo $row_Recordset3['item_img']?>"><?php echo $row_Recordset3['item_img']?></option>

$row_Recordset3 是一个包含值的数组。因此,如果您回显数组的值,它将显示为数组,而选项 block 的值将变为数组。

因此您需要将选项值中的$row_Recordset3更改为$row_Recordset3['item_img']

希望这有帮助:)

关于php - 表单中下拉框的数据作为 'Array' 发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130747/

相关文章:

php - Laravel 5 路由问题 - 错误方法调用异常

php - 如何从返回 403 Forbidden 的站点的 PHP 中获取 URL 的标题

javascript - 创建一个 html 表单并使用 javascript 访问其中的 python 脚本?

javascript - 启动时以 PDF 形式启动 JavaScript(使用 hyperref 生成)

php - 如何从 MySQL (PHP) 检索特定值

php - $_data 与 $this->_data - PHP

PHP HTML 表 - 用于填充下一页表的数据库元素的 href 链接

mysql - 在目标表具有一对一的情况下使用 hasManyThrough

php - 在 while 循环中访问第二个表

php - PHP 中的 new MyClass() 与 new MyClass()