您的SQL语法中出现错误"第1行和第34行;

时间:2014-05-05 23:07:34

标签: php

这里有很多问题,但我似乎无法找到问题......

我想使用php将我的FormMail.php提交到MySQL数据库。

获取错误:

错误=插入失败:您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在'' FullName',' EmailAddr',' contact',&##附近使用39;颜色',' vehicle',' mesg',' email',' rnam'在第1行

<?php
$con = mysql_connect("localhost","root","db79ax4");
if (!$con)
{
Error('database','Could not connect: ' . mysql_error()); /* this also exits the script */
}
$FullName = mysql_real_escape_string($aCleanedValues['FullName'],$con);
$EmailAddr = mysql_real_escape_string($aCleanedValues['EmailAddr'],$con);
$contact = mysql_real_escape_string($SPECIAL_VALUES['contact'],$con);
$colors = mysql_real_escape_string($SPECIAL_VALUES['colors'],$con);
$vehicles = mysql_real_escape_string($SPECIAL_VALUES['vehicles'],$con);
$mesg = mysql_real_escape_string($SPECIAL_VALUES['mesg'],$con);
$email = mysql_real_escape_string($SPECIAL_VALUES['email'],$con);
$rname = mysql_real_escape_string($SPECIAL_VALUES['rname'],$con);
mysql_select_db("resourcentr", $con);
$sql="INSERT INTO johnwork ('FullName', 'EmailAddr', 'contact', 'colors', 'vehicles', 'mesg', 'email', 'rname')
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";
if (!mysql_query($sql,$con))
{
Error('database','Insert failed: ' . mysql_error()); /* this also exits the script */
}
mysql_close($con);

有什么想法吗?上面的代码有问题吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

在INSERT INTO语句中,您的列名称周围不需要单引号'

$sql="INSERT INTO `johnwork` (`FullName`, `EmailAddr`, `contact`, `colors`, `vehicles`, `mesg`, `email`, `rname`)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";

答案 1 :(得分:2)

改变这个:

$sql="INSERT INTO johnwork ('FullName', 'EmailAddr', 'contact', 'colors', 'vehicles', 'mesg', 'email', 'rname')
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";

对此:

$sql="INSERT INTO johnwork (FullName, EmailAddr, contact, colors, vehicles, mesg, email, rname)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";

所以你的代码看起来像这样:

<?php
$con = mysql_connect("localhost","root","db79ax4");
if (!$con)
{
Error('database','Could not connect: ' . mysql_error()); /* this also exits the script */
}
$FullName = mysql_real_escape_string($aCleanedValues['FullName'],$con);
$EmailAddr = mysql_real_escape_string($aCleanedValues['EmailAddr'],$con);
$contact = mysql_real_escape_string($SPECIAL_VALUES['contact'],$con);
$colors = mysql_real_escape_string($SPECIAL_VALUES['colors'],$con);
$vehicles = mysql_real_escape_string($SPECIAL_VALUES['vehicles'],$con);
$mesg = mysql_real_escape_string($SPECIAL_VALUES['mesg'],$con);
$email = mysql_real_escape_string($SPECIAL_VALUES['email'],$con);
$rname = mysql_real_escape_string($SPECIAL_VALUES['rname'],$con);
mysql_select_db("resourcentr", $con);
$sql="INSERT INTO johnwork (FullName, EmailAddr, contact, colors, vehicles, mesg, email, rname)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";
if (!mysql_query($sql,$con))
{
Error('database','Insert failed: ' . mysql_error()); /* this also exits the script */
}
mysql_close($con);