簡體   English   中英

如何添加到數據庫中的現有值

[英]How to add to an existing value in the database

我回到另一個絕對的初學者問題,我有值存儲在數據庫中,我的目標是允許用戶使用下拉菜單減去或添加到特定的數據庫值(數量),值范圍從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將是最好的解決方案但是我不確定它應該如何實現,我也考慮連接但我我不確定它能否達到我的需要,你可以提供任何幫助這個新手表示贊賞。 謝謝

要使以下解決方案起作用,您需要在數據庫中插入一個值,例如partnumber = 1和qantity = 0.您肯定需要修改它以使用您的mysql連接字符串,並且需要在之后查看數據庫檢查它是否有效或添加選擇查詢。 但是,這將使用下拉列表中選擇的數量更新庫存物料1的數量...

file.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>

注意:這可以很容易地擴展,通過添加另一個下拉列表或輸入框來指定要在數據庫中更新的部分號,捕獲已發布的值並將其傳遞到Update查詢的where部分。

這沒有任何確認! 它還使用折舊的MySQL連接函數,請查找MySQLi和PDO

要將行更新為新的KNOWN值:

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

要更新添加到現有值的行,即向庫存添加額外的5個單位:

UPDATE inventory SET quantity = quantity + $IncramentalValue WHERE partnumber = $PartNumber;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM