dongshen7407 2017-03-10 22:30 采纳率: 0%
浏览 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线