douyue1998 2015-06-03 11:17
浏览 33

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

I wrote the following code:

<?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;
        }
    }
?>

What I want this script to do is select the quantity where the id is $insert and then if quantity > 0, add 1 to the quantity and update the quantity, if not insert the id and quantity = 1 into the table. The table has only two fields id(VARCHAR(32)) and quantity(DECIMAL(8,1)). I tried to do it more general in order to help as many people as possible. Could you please help me? Thanks in advance. NOTE: When I run the script in the browser(after uploading it to the server with the correct username,domain etc.) nothing shows up and I dont even get an error in the console.

  • 写回答

2条回答 默认 最新

  • dongyou6909 2015-06-03 11:52
    关注

    You need to extract the result of your query and loop through each row:

    <?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);
    
        while($row = mysqli_fetch_array($result)) {
            if ($row['quantity'] > 0) {
                $new_quantity = $row['quantity'] + 1;
                $sql2 = "UPDATE Eshop SET quantity = '$new_quantity' 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;
                }
            }
    
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了