doutangkao2789 2014-06-28 15:15
浏览 39
已采纳

在我的PHP脚本中找不到错误。 有人可以向我指出

I have written a php script on my web server to insert values into the table table3. The variables used to get values are username and image. username contains varchar type data and image contains text type data in it. I need to insert it into my table table3 . The table3 is having two columns username and imagename which is of varchar type and text type respectively.

When I try to run the above script by entering values, an error shows as given below:

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.

I don't understand what the error is and I'm stuck here with knowing the error. Can someone please clear the errors for me. I'm a newbie in php and doesnot know much about php. So a little help from anyone is needed... Please help me out. My php script is shown below:

<?php 
$con=mysqli_connect("localhost","username","password","db_name"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$username = $_POST['username']; 
$image = $_POST['image']; 
$result = mysqli_query($con,"INSERT INTO table3 (username,imagename) VALUES ('$username','$image')"); 
if (!mysqli_query($con,$result)) 
{
    die('Error: ' . mysqli_error($con));
}
else
    echo "1 record added";
mysqli_close($con);
?>
  • 写回答

4条回答 默认 最新

  • dongluxin6711 2014-06-28 15:23
    关注

    1) **You have an error in your SQL syntax; ** means that You have error in your query. It seems that your query is okay but that error may come from your post data. you need to mysqli_real_escape_string for post data.

    2) you have executed twice the query.

    try like this :

    $username = mysqli_real_escape_string($con, $_POST['username']); 
    $image = mysqli_real_escape_string($con,$_POST['image']); 
    $result = mysqli_query($con,"INSERT INTO table3 (username,imagename) VALUES ('$username','$image')"); 
    if (!$result) 
    {
        die('Error: ' . mysqli_error($con));
    }
    else
        echo "1 record added";
    mysqli_close($con);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作