我的表单包含输入字段item id
,staff id
和quantity
。我想在按 issue 时更新表格。我想要我提交的值,即只有当它小于或等于目标表中已有的数量值时才减去的数量。
以下是我的代码。
表格:
<?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'>   ORDER 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);
?>
表单操作:
<?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";
}
?>
答案 0 :(得分:0)
1) <form>
不允许在<table>
内。查看form-inside-a-table
2)您必须为所有详细信息保留一个提交按钮。做任何改变并提交。
3)根据第2点,输入的name
必须是数组类型。 (检查下面的答案)
4)在 Updateisue.php 中,使用for loop
或foreach
查找每个ItemId
并执行查询。
5)在这一行if($Quantity<=Quantity){
中。我不知道你从哪里获得Quantity
价值。但是,仍然。我假设的是:Quantity
针对该特定ItemId
。所以,我写了一个查询来执行查找数量。
<?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'>   ORDER 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 强>
<?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";
}
}
?>