有 PDO 经验的人可以帮助我吗?我已经花了大约 6 个小时,但我仍然无法运行查询。
这是我的代码:
<?php
include('database.php');
//print_r($_GET);
if(isset($_GET['ctype']))
{
$ctype = $_GET['ctype'];
}
else
{
die("Could determine type of input");
}
if($ctype==1)
{
//it is client
if((isset($_GET['uid'])) && (isset($_GET['name'])) && (isset($_GET['address'])) && (isset($_GET['city'])) && (isset($_GET['state'])) && (isset($_GET['zip'])))
{
try
{
$conn = new PDO("mysql:host=$host;dbname=$database", $username, $password);
//$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO clients (uid, name, address, city, state, zip) VALUES(:uid, :name, :address, :city, :state, :zip)";
$stmt = $conn->prepare($sql);
$stmt->execute(array(
':uid' => $_GET['uid'],
':name' => $_GET['name'],
':address' => $_GET['address'],
':city' => $_GET['city'],
':state' => $_GET['state'],
':zip' => $_GET['zip']
));
echo $stmt->rowCount(); // 1
}
catch(Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
}
}?>
database.php 是:
<?php
//Please Enter database settings here
$username = "root";
$password = "";
$database="letters";
$host="localhost";
?>
我用这个 GET 请求点击了页面
http://localhost/letters/contact_add.php?ctype=1&uid=mmm&name=test&address=6182&city=xyz&state=abc&zip=123456
我得到的错误:
Parse error: in C:\xampp\xampp\htdocs\letters\contact_add.php on line 26
第 26 行是:
$sql = "INSERT INTO clients (uid, name, address, city, state, zip) VALUES (:uid, :name, :address, :city, :state, :zip)";
我浏览了几乎所有可以在谷歌上获得的链接,但仍然无法找到问题所在。我遵循的链接是这个。我正在运行 xampp 1.8.1 并且 extension=php_pdo_mysql.dll 已启用。