简体   繁体   English

仅当字段中的值等于或大于输入值时,才如何从MySQL表的值中减去用户输入值?

[英]How can I subtract a user input value from a value in a MySQL table only if the value in the field is equal to or greater than the input value?

I have a form which contains input fields item id , staff id , and quantity . 我有一个包含输入字段item idstaff idquantity的表单。 I want to update a table whenever is press issue . 我想在新闻发布时更新表格。 I want the value I submit, that is the quantity to be subtracted only if it is less than or equal to the quantity value already in the destination table. 我想要我提交的值,即仅当其小于或等于目标表中已经存在的数量值时才减去数量。 Below is my code. 下面是我的代码。

The form: 表格:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("dbtest", $con);

$result = mysql_query ("SELECT * FROM recieved_orders");
echo "<table border = '1' style='margin-left:18px;margin-right:18px;' bgcolor='#CFC'>
                <tr>
                    <th bgcolor='#34495E' colspan='9'>
                        <h1><font color='white' align='center'>&nbsp&nbsp&nbspORDER OFFICE SUPPLIES</font></h1>
                    </th>
                </tr>
                <tr bgcolor='#CFC' font size='18'>
                    <th>Item Id</th>
                    <th>Staff Id</th>
                    <th>Quantity</th>
                </tr>";

        while ($row = mysql_fetch_array($result))
        {
            echo "<form action=\"Updateisue.php\" method=\"post\" enctype=\"multipart/form-data\">";
                echo "<tr>";
                    echo "<td><input type=\"text\" name=\"ItemId\"  size=\"30\" value=\" ". $row ['ItemId'] . "\" readonly></td>";
                    echo "<td><input type=\"text\" name=\"StaffId\" value=\" ". $row ['StaffId'] . "\" readonly></td>";
                    echo "<td><input type=\"text\" name=\"Quantity\" value=\" ".$row ['Quantity'] . "\" readonly></td>";
                    echo "<td><input type=\"submit\" name=\"submit\" size=\"30\" style='background-color:#3366FF' value=\"ISSUE  \"></td>";
                echo "</tr>";
            echo "</form>";
        }
echo "</table>";
mysql_close($con);
?>

Form action: 表单动作:

<?php
include './database-config.php';
$searchError = "";
$searchMessage = "";

function sanitizeString($var) {
    $var = htmlentities($var);
    $var = strip_tags($var);
    $var = stripslashes($var);
    $var = trim($var);
    return $var;
}
$ItemId = sanitizeString($_POST['ItemId']);
$Quantity = sanitizeString($_POST['Quantity']);

if($Quantity<=Quantity){
    $updatePassQuery = "UPDATE stationery SET Quantity=Quantity-$Quantity WHERE ItemId='$ItemId'";
    $executeQuery = mysqli_query($dbh,$updatePassQuery);
if($executeQuery){
    echo " update successful";
    $message  = "update was successful";
    header("location: procurementhome.php");

    } else{
        echo "unsuccessful";
        $error = "update failed";
        // header("location: upstationery.php");
    }
}
else
{
    echo "no more itmes";
}
?>

1) <form> is not allowed inside a <table> . 1) <form> <table>内不允许使用<form> <table> Check form-inside-a-table 查看表格中的表格

2) You have to keep one submit button for all the details. 2)您必须保留一个提交按钮以获取所有详细信息。 Do whatever changes there and submit. 在那里进行任何更改并提交。

3) According to Point 2, name of input have to be array type. 3)根据要点2,输入name必须为数组类型。 (Check answer below) (检查下面的答案)

4) In Updateisue.php , using for loop or foreach find each ItemId and execute query. 4)Updateisue.php中 ,使用for loopforeach查找每个ItemId并执行查询。

5) In this line if($Quantity<=Quantity){ . 5)在这一行中if($Quantity<=Quantity){ I don't know from where you get Quantity value. 我不知道您从哪里获得“ Quantity值。 But, still. 但是,仍然。 What i assumed is : Quantity for that particular ItemId . 我假设的是:该特定ItemId Quantity So, I wrote one query to execute to find quantity. 因此,我编写了一个查询以执行查找数量。

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("dbtest", $con);
$result = mysql_query ("SELECT * FROM recieved_orders");

echo "<form action=\"Updateisue.php\" method=\"post\" enctype=\"multipart/form-data\">";
    echo "<table border = '1' style='margin-left:18px;margin-right:18px;' bgcolor='#CFC'>
                    <tr>
                        <th bgcolor='#34495E' colspan='9'>
                            <h1><font color='white' align='center'>&nbsp&nbsp&nbspORDER OFFICE SUPPLIES</font></h1>
                        </th>
                    </tr>
                    <tr bgcolor='#CFC' font size='18'>
                        <th>Item Id</th>
                        <th>Staff Id</th>
                        <th>Quantity</th>
                    </tr>";

            while ($row = mysql_fetch_array($result))
            {
                    echo "<tr>";
                        echo "<td><input type=\"text\" name=\"ItemId[]\"  size=\"30\" value=\" ". $row ['ItemId'] . "\" readonly></td>";
                        echo "<td><input type=\"text\" name=\"StaffId[]\" value=\" ". $row ['StaffId'] . "\" readonly></td>";
                        echo "<td><input type=\"text\" name=\"Quantity[]\" value=\" ".$row ['Quantity'] . "\" readonly></td>";
                        echo "<td><input type=\"submit\" name=\"submit\" size=\"30\" style='background-color:#3366FF' value=\"ISSUE  \"></td>";
                    echo "</tr>";
            }
            echo "<tr><td colspan='3'></td><td><input type=\"submit\" name=\"submit\" size=\"30\" style='background-color:#3366FF' value=\"ISSUE  \"></td></tr>";
    echo "</table>";
echo "</form>";

mysql_close($con);

?>

Updateisue.php Updateisue.php

<?php
include './database-config.php';
$searchError = "";
$searchMessage = "";

function sanitizeString($var) {
    $var = htmlentities($var);
    $var = strip_tags($var);
    $var = stripslashes($var);
    $var = trim($var);
    return $var;
}

$totalItem = sizeof($_POST['ItemId']);
$Quantity = $_POST['Quantity'];
for($i=0;$i<$totalItem;$i++) {

    $CItemId = sanitizeString($ItemId[$i]);
    $CQuantity = sanitizeString($Quantity[$i]);

    $quantityAvailable = mysqli_query("SELECT Quantity FROM stationery WHERE ItemId='$CItemId ");
    $row = mysqli_fetch_array($quantityAvailable,MYSQLI_ASSOC); 
    $quantityDB = $row['Quantity'];

    if($CQuantity<=$quantityDB){
        $updatePassQuery = "UPDATE stationery SET Quantity=Quantity-$CQuantity WHERE ItemId='$CItemId'";
        $executeQuery = mysqli_query($dbh,$updatePassQuery);
        if($executeQuery){
            echo " update successful";
            $message  = "update was successful";
            header("location: procurementhome.php");
        } else{
                echo "unsuccessful";
                $error = "update failed";
        }
    }
    else
    {
        echo "no more itmes";
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM