要使以下解决方案起作用,您需要在数据库中插入一个值,例如 partnumber = 1 和 qantity = 0。您肯定需要修改它以使用您的 mysql 连接字符串,并且需要在之后查看数据库检查它是否有效或添加选择查询。但是,这将使用下拉列表中选择的数量更新库存项目 1 的数量...
文件.php
<?php
include("db_connection_file.php"); // in here are the username and password etc for your mysql connection
if($_POST){ // the code in this block will ONLY fire if the form has been posted
$dropdown_contents = $_POST['drop']; // this is the value of the form item 'drop' that has been posted...
$part_no_from_form = $_POST['part_no'];
$query ="UPDATE inventory SET quantity = '".$dropdown_contents."' WHERE partnumber = '".$part_no_from_form ."'"; // this updates the database and sets the quantity to that posted where the part number is that in the form
$testResult = mysql_query($query) or die('Error, query failed');
echo "This worked";
}
?>
<form acion="file.php" method="post">
Quantity <select name="drop" id="drop">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select><br />
Part No. <select name="part_no" id="part_no">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select><br />
<input type="submit" value="go" />
</form>
注意:这可以很容易地扩展以指定要在数据库中更新的部件号,方法是添加另一个下拉列表或输入框,捕获发布的值并将其传递给更新查询的 where 部分。
这没有任何验证!它也在使用贬值的 MySQL 连接函数,请查阅 MySQLi 和 PDO