如何将mysql获取的数据再次插入到数据库表中

时间:2014-03-31 10:49:11

标签: php mysql arrays insert

我从数据库中获取这些杂货产品价值。客户可以在相应的文本框中输入其数量。现在,我想将这些值再次存储到具有数量值的数据库表中。我怎样才能做到这一点?我可以使用输入文本字段插入数据。但是,这里我有来自数据库的值。

如何再次插入这些数据?

    <form name="form1" action="" method="post" class="groceries-form">
        <?php 
        $sql_records = "SELECT * FROM ".tbl_groceries."";
        $records = mysql_query($sql_records,$CN);
        while($rows = mysql_fetch_array($records))
        {
         ?>
            <tr class="itemrow">
                 <td style="text-align: center;"><?=$rows['ID']?></td>
                 <td class="name"><span code="18" class="iname" type="text"><?=$rows['item_title']?></span></td>
                 <td class="qty"><input maxlength="2" class="tqty orderbox" type="text" name="quantity"></td>
                 <td class="qty1"><?=$rows['price']?></td>
                 <td class="qty1"><?=$rows['mrp']?></td>
                 <td class="qty1"><?=$rows['savings']?></td>
                 </tr>
   <?php } ?>
         <input type="submit" name="submit" value="Submit" class="submit"/>
         </form>  

3 个答案:

答案 0 :(得分:1)

我建议您使用PDO,否则您可能会遇到mysql注入问题。

PHP: PDO - Manual

如果您仍然希望使用与代码中相同的方式(不推荐使用),则基本上与输入相同。 post / get值就像你从数据库输出的那些变量一样。只需将这些值与输入值(在php中显示为变量)一样插入到数据库中。

编辑:

我想我误解了你的问题,但你应该检查PDO! :)

答案 1 :(得分:0)

您需要将数据发送到另一个php处理页面,同时添加一个表格以供新数据输入,foreach表格列不是数量,您需要在其中添加隐藏的输入形式如:
    <input type="hidden" name="itemid" value="<?=$rows['id']?>"> 这些是您可能希望包含在新表中的一些字段: 项目ID ITEMPRICE ItemQuantity CustomerID(如果您有客户ID或名称提供

使用以下php代码,您应该能够将新数据插入到其他mysql表中

<?php
$itemid = $_POST['itemid'];
$itemprice = $_POST['itemprice'];
$itemquantity = $_POST['quantity'];
$customerid = $_POST['customerid'];
mysql_query("INSERT INTO newtablename (itemid, itemprice, itemquantity, customerid) VALUES ('$itemid','$itemprice','$itemquantity','$customerid')");
?>

(别忘了连接数据库)

答案 2 :(得分:-1)

将表单发送到执行Update sql查询的php文件,传递为每条记录创建查询所需的值。

Mysql update with w3schools

相关问题