textarea'在HTML表内循环更新mysql数据库


Extracting values from 'textarea' inside loop in an html table to update the mysql database

我在mysql数据库中有一个名为'Spatula'的表。里面有数据。我通过使用"html表"的php代码在网页上显示此数据。表格的最后一列称为"订单数量",这是一个"文本区"。用户可以通过在文本区域内输入整数值来输入他们想要的Spatulas的数量。问题是,我不知道如何选择所有的值从文本区域,以便我可以使用它来更新我的数据库。我要做的是根据用户输入更新mysql表中的最后一行。例如,如果用户订购了2个id为3的抹刀,则应该从mysql表中扣除2个抹刀。你能告诉我如何从我的代码从文本区提取数据,并使用它来更新我的数据库吗?

下面是我的代码,显示从数据库的值,并显示在网页上:

<html>
<Title>
  WebPage1
</Title>
<head>
<p><h1> Orders </h1></p>
</head>
<body>
<p>Customer Details: </p>
  <textarea rows="6" cols="60">
  </textarea>
  <br>
Responsible Staff Member:
<textarea rows="0.5" cols="4" style="width: 188px; height: 22px;">
</textarea>
<p> </p>
<?php
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","sjayswal","sjayswal_2016","sjayswal");
// Check connection
if (mysqli_connect_errno()) {
    echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}

/* this lists the name and release date of Contents from Spatula */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT * from Spatula");
echo "<tr>";
        echo "<td>idSpatula</td><td>ProductName</td><td>Type</td><td>Size</td><td>Colour</td><td>Price</td><td>Quantity currently in stock</td>
    <td>Order Quantity</td>";
        echo "</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['idSpatula'] . "</td><td>" . $row['ProductName'] . "</td><td>".$row['Type']."</td><td>". $row['Size'].
    "</td><td>".$row['Colour']."</td><td>".$row['Price']."</td><td>"
    .$row[' QuantityInStock']."</td><td> <input type= 'textarea' name='textput' id='spatid' value='0'></textarea></td>";
    echo "</tr>";

}
echo "</table>";
echo "</br>";
echo '<input type="submit" value="Submit">';

if (isset($_POST['Submit'])) {
$values=$_POST['textput'];
}



mysqli_close($con);
?>



</body>

</html>
这是我的数据库的样子

PHP和html "在两个不同的线程中运行"服务器将生成html的第一部分然后执行PHP代码然后生成最后一部分,所以你不能将用户输入传递给PHP代码,你需要对服务器做一些类似ajax调用的操作将用户输入的数据保存在db

试一下

<input type='hidden' name='ids[]' value='".$row['idSpatula']."'>
<textarea name='textput".$row['idSpatula']."'  value='0'></textarea>

while循环

if (isset($_POST['Submit'])) {
    foreach($ids as $id)
    {
        $text_box_value = $_POST['textput'.$id];
        //write code for updation
    }
}