-1

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

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); 

    }

请你能帮我解决这个问题,我知道我错过了一些愚蠢的事情......

霍克斯顿

4

2 回答 2

2

没有取代 h2ooooooo 的好地方和回复,只是说你可能会一遍又一遍地犯同样的错误。避免这种情况的一种方法是通过格式化使您的代码更具可读性:

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

是的,这意味着更多的 LOC,但它也会减少花在追逐愚蠢错误上的时间,因为缺少前导逗号会跳出页面。

还有一个小好处是您可以很容易地在页面上发现您的 SQL 语句。

(是的,这是评论而不是答案,但这是我能说明我观点的唯一方法)

于 2013-10-14T09:01:57.787 回答
1

您在:dobyearand之间缺少逗号:gender

$STH = $dbh->prepare("INSERT INTO users (firstname, lastname, company, jobtitle, usertype, address, email, phone, 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)");

于 2013-10-14T08:31:05.230 回答