繁体   English   中英

PHP / Pdo插入不起作用:语法错误?

[英]Php/Pdo insert not working: syntax error?

我正在编写一些代码,以使用PDO将一些数据输入到mysql dtabase中。 该代码有效(验证数据是否全部存在):

echo $usertype . '<br>';
echo $firstname . '<br>';
echo $lastname . '<br>';
echo $company . '<br>';
echo $jobtitle . '<br>';
echo $address . '<br>';
echo $email . '<br>';
echo $telephone . '<br>';
echo $mobile . '<br>';
echo $dobday . '<br>';
echo $dobmonth . '<br>';
echo $dobyear . '<br>';
echo $gender . '<br>';
echo $ethnicity . '<br>';
echo $password . '<br>';
echo $credits . '<br>';

但是这段代码没有(我已经检查了数据库连接,并且确实连接了,并且名为users的表确实存在):

$STH = $dbh->prepare("INSERT INTO users (firstname, lastname, company, jobtitle, usertype, address, email, telephone, mobile, dobday, dobmonth, dobyear, gender, ethnicity, password, credits) values (:firstname, :lastname, :company, :jobtitle, :usertype, :address, :email, :telephone, :mobile, :dobday, :dobmonth, :dobyear :gender, :ethnicity, :password, :credits)");
$STH->bindParam(':firstname', $firstname);
$STH->bindParam(':lastname', $lastname);
$STH->bindParam(':company', $company);
$STH->bindParam(':jobtitle', $jobtitle);
$STH->bindParam(':usertype', $usertype);
$STH->bindParam(':address', $address);
$STH->bindParam(':email', $email);
$STH->bindParam(':telephone', $telephone);
$STH->bindParam(':mobile', $mobile);
$STH->bindParam(':dobday', $dobday);
$STH->bindParam(':dobmonth', $dobmonth);
$STH->bindParam(':dobyear', $dobyear);
$STH->bindParam(':gender', $gender);
$STH->bindParam(':ethnicity', $ethnicity);
$STH->bindParam(':password', $password);
$STH->bindParam(':credits', $credits);
try{
    $STH->execute();
    redirect_to(formsuccess.php);
    }
catch(PDOException $e) { 
    echo "I'm sorry, Dave. I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
    redirect_to(databaseentryfail.php); 

    }

请您能帮我这个忙,我知道我错过了这很愚蠢的事情...

霍克斯顿

不能代替h2ooooooo的最佳地方和答复,而只是说您很可能会一遍又一遍地犯同样的错误。 避免这种情况的一种方法是,通过设置其格式使代码更具可读性:

$STH = $dbh->prepare("INSERT INTO users (
  firstname
, lastname
, company
, jobtitle
etc

是的,这意味着更多的LOC,但它会减少花费在寻找愚蠢错误上的时间,因为缺少前置逗号会跳到您的页面上。

还有一个轻微的好处,就是您可以轻松地在页面上发现SQL语句。

(是的,这不是评论,而是答案,这是我可以说明我观点的唯一途径)

您缺少:dobyear:gender之间的逗号:

$ STH = $ dbh-> prepare(“ INSERT INTO用户(名字,姓氏,公司,职位,用户类型,地址,电子邮件,电话,手机,dobday,dobmonth,dobyear,性别,种族,密码,信用)值(:firstname ,:lastname,:company,:jobtitle,:usertype,:address,:email,:telephone,:mobile,:dobday,:dobmonth, :dobyear:gender ,:ethnicity,:password,:credits)“)”;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM