dongshen7407 2017-03-10 22:30
浏览 69
已采纳

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 2017-03-14 04:04
    关注
    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效