0

I'm back with another absolute beginner question, I have values stored in a database, my goal is to allow users to either subtract or add to a specific database value (quantity) using a drop down menu with values from ranging from 1 - 10. I'm keeping it simple for now.

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);

I can find plenty of examples just adding or deleting a static value however that's not what I need. I've searched around and few examples resembling what I'm looking for 1 & 2 It seems as though UPDATE would be the best solution however I'm not sure exactly how it should be implemented, I've also looking into concatenation but I'm not sure it can accomplish what I need, any assistance you can offer this novice is appreciated. Thanks

4

2 に答える 2

2

次のソリューションを機能させるには、たとえば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>

注:これは、データベースで更新する部品番号を指定するために簡単に拡張できます。たとえば、別のドロップダウンまたは入力ボックスを追加し、投稿された値をキャッチして、更新クエリの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 に答える