【发布时间】:2012-09-09 18:42:25
【问题描述】:
我正在尝试开始掌握 PHP 和 PDO。刚入手,听说 PDO 比较好用,所以我不使用比较流行的 mysqli。
我无法使用 PDO 将数据插入到我的数据库中。我想尝试使用准备好的语句来最小化 sql 注入。我尝试了很多方法,一直碰壁。
请大家指点一下。
这是错误信息。
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\wamp\www\test-search\insert_property.php on line 61
这是相关的php代码。
// 插入属性
// 包含数据库
包括“db_connect_pdo.php”;
包括'defines.php';
尝试 {
// 创建 SQL
$sql = "INSERT INTO 属性(pid、title、intro_en、description_en、卧室、
浴室、地址、城市、国家、stype、ptype、价格)
值(:pid,:title,:intro_en,:description_en,:卧室,:浴室,:地址,
:city, :country, :stype, :ptype, :price)";
//准备语句
$stmt = $conn->prepare($sql);
// 绑定参数并执行语句
$stmt->bindValue(':pid', $pid);
$stmt->bindValue(':title', $title);
$stmt->bindValue(':intro_en', $intro_en);
$stmt->bindValue(':description_', $description_en);
$stmt->bindValue(':bedrooms', $bedrooms);
$stmt->bindValue(':bathrooms', $bathrooms);
$stmt->bindValue(':address', $address);
$stmt->bindValue(':city', $city);
$stmt->bindValue(':country', $country);
$stmt->bindValue(':stype', $stype);
$stmt->bindValue(':ptype', $ptype);
$stmt->bindValue(':price', $price);
// 执行语句
$stmt->执行();
}
捕获(异常 $e)
{
回声 $e->getMessage();
}
$conn = 空;
?>
不知道哪里出了问题,任何建议将不胜感激。
【问题讨论】:
-
记住你也可以这样做 $stmt->execute(array('pid' => $pdi, 'title' => $title, 'intro_en'...));而不是一一绑定值。我想说,你也可以使用问号来减少编码(但可能更令人困惑)。