【发布时间】:2014-04-20 12:22:28
【问题描述】:
我对 php 和 mysql 比较陌生。我在这部分代码中尝试做的是根据 2 个文本框的输入更新数据库中的 2 列信息。每当我尝试更新数据库中的值时,它们都会更新为 0。我在声明 $ 变量后放置了 echo 语句,这两个变量的值与我在框中输入的值相同。但是当我运行 sql_query 时,数据库中的新值是 0 而不是 2 $ 变量的值。任何帮助将非常感激!提前致谢!
<?php
$result = mysql_query("SELECT * from place_order ORDER BY item_name;");
echo "<form action='' method='POST'>Select an item:<select name='selection'><option>Select...</option>";
while ($row = mysql_fetch_assoc($result))
{
$item_name = $row["item_name"];
echo "<option>$item_name</option>";
}
echo "</select>
<input type='submit' value='Select Item' style='float:right;'/>";
$selection = $_POST['selection'];
echo "<br><br>Type the updated information into the text fields<br><br>";
echo "
<table width='300'>
<tr>
<td align='left'>Item Cost(€): </td>
<td align='left'><input type='text' name='cost'></td>
</tr>
<tr>
<td align='left'>Item Quantity: </td>
<td align='left'><input type='text' name='quantity'></td>
</tr>
<tr>
<td align='left'></td>
<td align='left'><input type='submit' name='button' value='Submit'></td>
</tr>
</table>
";
$cost = $_POST['cost'];
$quantity = $_POST['quantity'];
$selection = $_POST['selection'];
$sql = "UPDATE place_order SET item_cost='$cost', quantity='$quantity' WHERE item_name='$selection' ORDER BY item_name;;";
$query_update = mysql_query($sql);
if($query_update)
{
echo "Table updated! Click View Stock in the menu to view the updated table";
}
echo"</form>";
?>
【问题讨论】:
-
简单:
$select未定义。 -
列上的
datatype是什么? $_POST 元素的值是什么? -
您的代码容易受到SQL injection的攻击
-
@fred-ii- 这是我定义 $select 的 php 脚本的开始: $result = mysql_query("SELECT * from place_order;"); echo ""; $select = $_POST['select'];
-
@edvinas.me item_cost 的数据类型是小数(6,2),数量列是整数。 $_POST 元素的值取决于文本框中的内容。提交信息时,两个值正常回显,但当输入数据库时,由于某种原因,它们变为 0