使用implode()插入值和错误时出现意外错误

时间:2013-11-15 13:18:07

标签: php mysql pdo

我使用PDO插入值,但我收到的错误为:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'desc,price,nick_name,gender,size,color,birth_date,uname,uphone,ucountry,ustate,u' at line 1' in C:\wamp\www\aa\abc.php:58 Stack trace: #0 C:\wamp\www\www\aa\abc.phpphp(58): PDOStatement->execute(Array) #1 {main} thrown in C:\wamp\www\www\aa\abc.php.php on line 58

还获得了内置函数的Warning: implode() [function.implode]: Bad arguments

代码:

foreach ($_POST['pcheck'] as $p_check) ////storing checkbox values
{
    $pcheckp[] = $p_check;
}   $finalcheck = implode(',', $pcheck);
foreach ($_POST['pinc'] as $p_inc) ////storing inputfield values
{
    $pinc[] = $p_inc;
}   $finalpinc = implode(',', $pinc);

$sql = "INSERT INTO list (u_id,list_type,list_ff,breed,title,desc,price,nick_name,gender,size,color,birth_date,uname,uphone,ucountry,ustate,ucity,usite,pcheck,pinc,photo)
VALUES(:uid,:list_type,:list_ff,:breed,:title,:desc,:price,:nick_name,:gender,:size,:color,:date,:uname,:uphone,:ucountry,:ustate,:ucity,:usite,:pcheck,:pinc,:p_photo)";
$q = $db->prepare($sql);
$q->execute(array(':uid'=>dd,
                ':list_type'=>$list_type,
                ':breed'=>$breed,
                ':title'=>$title,
                ':desc'=>$desc,
                ':price'=>$price,
                ':list_ff'=>$list_ff,
                ':nick_name'=>$nick_name,
                ':gender'=>$gender,
                ':size'=>$size,
                ':color'=>$color,
                ':date'=>$date,
                ':uname'=>$uname,
                ':uphone'=>$uphone,
                ':ucountry'=>$ucountry,
                ':ustate'=>$ustate,
                ':ucity'=>$ucity,
                ':usite'=>$usite,
                ':pcheck'=>$finalcheck,
                ':pinc'=>$finalpinc,
                ':p_photo'=>$p_photo));

$_POST['pcheck']$_POST['pinc']用于获取我将要存储在mysql列中的复选框和输入值。

我已经多次检查过在插入查询中发现语法错误但是没有错误

希望得到帮助 谢谢!

1 个答案:

答案 0 :(得分:2)

代表Warning: implode()

$finalcheck = implode(',', $pcheck);

应该是

$finalcheck = implode(',', $pcheckp);

还为mysql保留了desc,你需要将它与`

一起使用
    $sql = "INSERT INTO list (`u_id`,`list_type`,`list_ff`,`breed`,`title`,`desc`,`price`,`nick_name`,`gender`,`size`,`color`,`birth_date`,`uname`,`uphone`,`ucountry`,`ustate`,`ucity`,`usite`,`pcheck`,`pinc`,`photo`)
VALUES(:uid,:list_type,:list_ff,:breed,:title,:desc,:price,:nick_name,:gender,:size,:color,:date,:uname,:uphone,:ucountry,:ustate,:ucity,:usite,:pcheck,:pinc,:p_photo)";