// Not working
$stmt = $connection->prepare( "INSERT INTO numbers (homePhone, mobilePhone, officePhone)".
" VALUES ($phone_1,$phone_2,$phone_3)");
$stmt->execute();
// Works
$stmt = $connection->prepare( "INSERT INTO numbers (homePhone, mobilePhone, officePhone)".
" VALUES (?,?,?)");
$stmt->execute([$phone_1, $phone_2, $phone_3]);
执行第一个时,会输出错误:
致命错误:未捕获PDOException:SQLSTATE [HY000]:常规错误:1 没有这样的专栏:blablabla in C:\ Users \ zahha \ IdeaProjects \ icd0007 \ index.php:78堆栈跟踪:#0 C:\ Users \用户zahha \ IdeaProjects \ icd0007 \的index.php(78): PDO->准备(' INSERT INTO peo ...')#1 {main}抛出 第78行的C:\ Users \ zahha \ IdeaProjects \ icd0007 \ index.php
第二个完美无缺。问题是什么?只是想知道。
答案 0 :(得分:1)
您需要在第一个变量中引用变量,以指示值是SQL中的字符串文字。
$stmt = $connection->prepare( "INSERT INTO numbers (homePhone, mobilePhone, officePhone)".
" VALUES ('$phone_1','$phone_2','$phone_3')");
答案 1 :(得分:-1)
要使第一个工作,你应该把变量放在'。例如:
$stmt = $connection->prepare( "INSERT INTO numbers (homePhone, mobilePhone, officePhone)".
" VALUES ('$phone_1','$phone_2','$phone_3')");
$stmt->execute();
或者将它们从字符串中删除,例如:
$stmt = $connection->prepare( "INSERT INTO numbers (homePhone, mobilePhone, officePhone)".
" VALUES ('".$phone_1."','".$phone_2."','".$phone_3."')");
$stmt->execute();