dtwxt88240 2014-10-20 17:00
浏览 45
已采纳

MySQL语法错误说“接近'1'”但我的查询中没有'1'

I'm making a website for the first time using PHP with a MySQL database and getting a syntax error that I don't understand. I just want to delete everything from tblreparation with a certain ID and also everything from rblrepstat with that same ID. I am using this code:

<?php
    $con=mysqli_connect("localhost","root","MYPASS","repair");
    $ID = $_REQUEST['ID'];
    $sql = mysqli_query($con, "DELETE FROM tblreparation WHERE ID = {$ID}");
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    $sql = mysqli_query($con, "DELETE FROM tblrepstat WHERE repID = {$ID}");
    if (!mysqli_query($con,$sql)) {
      die('Error2: ' . mysqli_error($con));
    }
    echo "1 record deleted";
    mysqli_close($con); 
?>

And this is the error I am getting:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Near 1? I don't even see a '1'...

  • 写回答

2条回答 默认 最新

  • douchuo9476 2014-10-20 17:18
    关注

    You're calling mysqli_query twice for each query.

    The second time you call it you're actually passing a resource as the query parameter, which causes the error you're getting.

    Try changing your code to this:

    <?php
        $con=mysqli_connect("localhost","root","MYPASS","repair");
        $ID = $_REQUEST['ID'];
        $sql = "DELETE FROM tblreparation WHERE ID = {$ID}";
        if (!mysqli_query($con,$sql)) {
          die('Error: ' . mysqli_error($con));
        }
        $sql = "DELETE FROM tblrepstat WHERE repID = {$ID}";
        if (!mysqli_query($con,$sql)) {
          die('Error2: ' . mysqli_error($con));
        }
        echo "1 record deleted";
        mysqli_close($con); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊