选择,插入和更新MySQL表PHP脚本不起作用

时间:2015-06-03 11:17:44

标签: php mysql select insert sql-update

我写了以下代码:

<configuration>
...
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

我希望此脚本执行的操作是选择<?php $servername = "domain"; $insert = 12345678; $username = "user"; $password = "password"; $dbname = "database"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT quantity FROM Eshop WHERE id = $insert"; $result = $conn->query($sql); echo $result; if ($result > 0) { $result = $result + 1; $sql2 = "UPDATE Eshop SET quantity = $result WHERE id = $insert"; if ($conn->query($sql2) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql2 . "<br>" . $conn->error; } } else { $sql3 = "INSERT INTO Eshop (id, quantity) VALUES ($insert, 1)"; if ($conn->query($sql3) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql3 . "<br>" . $conn->error; } } ?> quantity所在的id,然后选择$insert,将{1}添加到quantity > 0更新quantity,如果没有将quantityid = 1插入表格中。该表格只有两个字段quantityid(VARCHAR(32))。为了帮助尽可能多的人,我试图做到更一般。请你帮助我好吗?提前致谢。注意:当我在浏览器中运行脚本时(在使用正确的用户名,域等将其上载到服务器之后)没有显示任何内容,我甚至都没有在控制台中收到错误。

2 个答案:

答案 0 :(得分:0)

您需要提取查询结果并遍历每一行:

sudo pip install ansible --upgrade

答案 1 :(得分:0)

    <?php
    $servername = "domain";
    $insert = 12345678;
    $username = "user";
    $password = "password";
    $dbname = "database";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT quantity FROM Eshop WHERE id = $insert";
    $result = $conn->query($sql);

    //check if particular record exists or not
    $count=mysql_num_rows($result);

   if($count>0) // if records exists for the particular id
   {
    while($row = mysqli_fetch_array($result)) {
        if ($row['quantity'] > 0) {
            $new_quantity = $row['quantity'] + 1;
            $update = "UPDATE Eshop SET quantity = '$new_quantity' WHERE id = '$insert'";
            if ($conn->query($update ) == TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $update . "<br>" . $conn->error;
            } 
        } else {
            $insert = "INSERT INTO Eshop (id, quantity) VALUES ('$insert', 1)";
            if ($conn->query($insert ) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $insert . "<br>" . $conn->error;
            }
        } // else qty is <=0

    } //end while
}
else {
   echo "Records do not exists for that particular id";
 }
?>