0

我带着另一个绝对初学者问题回来了,我将值存储在数据库中,我的目标是允许用户使用下拉菜单减去或添加到特定数据库值(数量),其值范围为 1 - 10 .我现在保持简单。

mysql_select_db("toner", $con);
$sql=INSERT INTO inventory (partnumber, quantity)
VALUES
('$_POST[partnumber]','$_POST[quantity]');
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);

我可以找到很多只是添加或删除静态值的示例,但这不是我需要的。我已经四处搜索了一些类似于我正在寻找的示例12似乎 UPDATE 将是最好的解决方案但是我不确定它应该如何实现,我也在研究连接但我'不确定它能否完成我的需要,感谢您为这个新手提供的任何帮助。谢谢

4

2 回答 2

2

要使以下解决方案起作用,您需要在数据库中插入一个值,例如 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

于 2012-11-01T09:24:26.343 回答
0

要将行更新为新的 KNOWN 值:

UPDATE inventory SET quantity = $KnownValue WHERE partnumber = $PartNumber;

要更新添加到现有值的行,即向库存添加额外的 5 个单位:

UPDATE inventory SET quantity = quantity + $IncramentalValue WHERE partnumber = $PartNumber;
于 2012-11-01T09:16:44.113 回答