我有一个使用PDO将数据插入数据库的注册表。
这是我的html文件;
https://codepen.io/anon/pen/pYOKJx
这是我的insert.php文件
<?php
//insert.php;
if(isset($_POST["cname"]))
{
$connect = new PDO("mysql:host=localhost;dbname=dbname", "testing", "pass");
for($count = 0; $count < count($_POST["cname"]); $count++)
{
$query = "INSERT INTO guest
(cname, sex, ic, age, nationality, phone, e_phone, address, check_in, check_out,remarks)
VALUES (:cname, :sex, :ic, :age, :nationality, :phone, :e_phone, :address, :check_in, :check_out, :remarks)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':cname' => $_POST["cname"][$count],
':sex' => $_POST["sex"][$count],
':ic' => $_POST["ic"][$count],
':age' => $_POST["age"][$count],
':nationality' => $_POST["nationality"][$count],
':phone' => $_POST["phone"][$count],
':e_phone' => $_POST["e_phone"][$count],
':address' => $_POST["address"][$count],
':check_in' => $_POST["check_in"][$count],
':check_out' => $_POST["check_out"][$count],
':remarks' => $_POST["remarks"][$count]
)
);
}
$result = $statement->fetchAll();
if(isset($result))
{
echo 'ok';
}
}
?>
我在错误日志中找不到任何错误,但是当我检查数据库时,没有插入数据。
我应该进行哪些更改才能使其正常工作?
答案 0 :(得分:0)
您是否尝试过更改:
$result = $statement->fetchAll();
if (isset($result)) {
echo 'ok';
}
到
if ($statement->fetchAll()) {
echo 'ok';
}
因为您的isset($result)
似乎没有执行该命令。