如何INSERT新行和UPDATE旧行值?

时间:2016-03-19 00:44:57

标签: php mysql

我想用我的PHP表单输入值

table 插入一个新行

$_POST['bill_amount']$_POST['paid_amount']$_POST['due_amount']

以及使用值" 0"更新第二行的 due_amount 列。

之后,我想选择 echo 最后一行(新插入的行)的值。

我的PHP代码:

<?php session_start(); ?>
<?php error_reporting(E_ALL ^ E_DEPRECATED); ?>

<body>

    <?php
        if(isset($_SESSION['username'])) {
    ?>

    <?php
        include 'dbconnection.php';
        $ba = $_POST['bill_amount'];
        $pa = $_POST['paid_amount'];
        $da = $_POST['due_amount']
        $sql = "INSERT into table(bill_amount,paid_amount,due_amount)
              values ('$ba','$pa','$da')";
             "UPDATE table SET due_amount='0' ORDER BY bill_id DESC LIMIT 1,1";

        if (!mysql_query($sql)) {
            die('Error:' . mysql_error());
        }

        echo "Here's the newly inserted record:";
        $result = mysql_query("SELECT * FROM table ORDER BY bill_id DESC LIMIT 1");  

        echo "<table>
                <tr>
                   <th>Bill ID</th>
                   <th>Bill Amount</th>
                   <th>Paid Amount</th>    
                   <th>Due Amount</th>
                </tr>";

        while($row = mysql_fetch_array($result)) {

            echo "<tr>";
            echo "<td>" . $row['bill_id'] . "</td>";
            echo "<td>" . $row['bill_amount'] . "</td>";
            echo "<td>" . $row['paid_amount'] . "</td>";
            echo "<td>" . $row['due_amount'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    ?>

    <HTML>

    <?php
        }
        else {
            echo "session expired";
        }
    ?>

    </body>
</html>

<?PHP
    mysql_close($con);
?>

我的PHP代码的问题是它成功地将新行插入,但是没有更新第二行的 due_amount 列价值&#34; 0&#34;

table 的第一个视图:
(请注意&#34; bill_id&#34;是auto increment primary key):

       +------------+-------------+---------------+---------------+  
       | bill_id    | bill_amount | paid_amount   | due_amount    | 
       +------------+-------------+---------------+---------------+
       | 1          | 200         | 100           | 100           |
       +------------+-------------+---------------+---------------+

执行我的PHP代码后的视图:

       +------------+-------------+---------------+---------------+  
       | bill_id    | bill_amount | paid_amount   | due_amount    | 
       +------------+-------------+---------------+---------------+
       | 1          | 200         | 100           | 100           |
       +------------+-------------+---------------+---------------+
       | 2          | 300         | 100           | 200           |
       +------------+-------------+---------------+---------------+

table 的视图我想在执行我的PHP代码之后拥有:

       +------------+-------------+---------------+---------------+  
       | bill_id    | bill_amount | paid_amount   | due_amount    | 
       +------------+-------------+---------------+---------------+
       | 1          | 200         | 100           | 0             |
       +------------+-------------+---------------+---------------+
       | 2          | 300         | 100           | 200           |
       +------------+-------------+---------------+---------------+

2 个答案:

答案 0 :(得分:0)

您需要在INSERT之前获取记录的ID。

另一个解决方案是使用last_insert_id()排除新生成的行,并从更新中省略此Id。

有些人喜欢这样:

 $sql = "INSERT into table(bill_amount,paid_amount,due_amount) values ('$ba','$pa','$da');
         UPDATE table SET due_amount='0'  WHERE bill_id != LAST_INSERT_ID() ORDER BY bill_id DESC LIMIT 1,1;";

小心:

  • SQL注入。
  • 您的代码可能会出现问题。

答案 1 :(得分:0)

你只需要分开你的查询:

 <?php 
 // ....
 $sql = "INSERT into table(bill_amount,paid_amount,due_amount)
          values ('$ba','$pa','$da')";

 $sql_update = "UPDATE table SET due_amount='0' ORDER BY bill_id DESC LIMIT 1,1";

 if(!mysql_query($sql))
 {
   die('Error:' . mysql_error());
 }

 if(!mysql_query($sql_update))
 {
   die('Error:' . mysql_error());
 }
 /....
 ?>

除此之外,你很容易受到 sql-injection 的攻击 请阅读有关准备好的声明以及如何避免sql-injection!