dongshen7407
dongshen7407
2017-03-10 22:30

SQL插入错误 - 使用PHP和MariaDB

已采纳

Here's the code:

            $coinno = $_POST["CoinNo"];
            $week = $_POST["week"];
            $payer = $_POST["payer"];
            $payee = $_POST["payee"];

            $servername = "localhost";
            $username = "username"; // Edited from original
            $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 = "INSERT INTO `Transactions`(`Week`,`Coin`,`Payer`,`Payee`)
                                      VALUES ($week,$coinno,'$payer','$payee')";
            if($conn->query(sql)===TRUE)
            {
                echo "Success";
            }
            else
            {
                echo "Error ".$sql."<br>".$conn->error;
            }
            $conn->close;
        ?>

And my error message:

Error INSERT INTO Transactions(Week,Coin,Payer,Payee) VALUE (1,2,'Bob','Carol') You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql' at line 1

I've tried using backticks, single quotes, etc. Even copying the generated query from phpMyAdmin did not help.

Another notable issue is that my Transactions table has a single row but when I try to select it I get 0 results. Could there possibly be a connection?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqm88684 dqm88684 4年前
    if($conn->query(sql)===TRUE)
    

    -->

    if($conn->query($sql)===TRUE)
    

    Also, get into the habit of flipping the if around -- some functions return either FALSE or something useful. That is, they may never return TRUE. That is:

    if($conn->query($sql) !== FALSE)
    
    点赞 评论 复制链接分享