我无法将记录插入到我的数据库中,我已经检查了代码并且 PHP 和数据库之间的所有变量和名称都匹配。
没有错误消息,我收到的文字说明预订已创建,但没有记录输入数据库。
这是插入记录的php代码;
<div class="containter">
<?php
if (isset($_POST['submit'])) {
try {
include ('include\PDO.php');
$sql = "INSERT INTO customers(Customer_Name, Customer_Email, Customer_Contact) VALUES (:Customer_Name, :Customer_Email, :Customer_Contact)";
//Named Parameters
$stmt = $dbh->prepare($sql);
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
$Customer_Name = filter_input(INPUT_POST, 'Customer_Name');
$stmt->bindValue(':Customer_Name', $Customer_Name, PDO::PARAM_STR);
$Customer_Email = filter_input(INPUT_POST, 'Customer_Email');
$stmt->bindValue(':Customer_Email', $Customer_Email, PDO::PARAM_STR);
$Customer_Contact = filter_input(INPUT_POST, 'Customer_Contact');
$stmt->bindValue(':Customer_Contact', $Customer_Contact, PDO::PARAM_STR);
print $Customer_Contact;
print $Customer_Name;
print $Customer_Email;
$stmt->execute();
$dbh = null;
} catch (PDOException $e) {
//Error Messages
print "We have had an error: " . $e->getMessage() . "<br/>";
die();
}
?>
<p> Booking Created.</p>
<?php } else { ?>
<form action ="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label>Name:</label> <input type="text" name ="Cusomer_Name">
<label>Email:</label> <input type="email" name ="Cusomer_Email">
<label>Contact:</label> <input type="tel" name ="Cusomer_Contact">
<input type="submit" name ="submit">
</form>
<?php } ?>
</div>
</body>
</html>
我已经检查了我能想到的所有内容,但我似乎无法将记录添加到数据库中。
对我做错了什么有什么想法吗?
最佳答案
在 html 表单中你有:
<label>Name:</label> <input type="text" name ="Cusomer_Name">
在您过滤 Customer_Name 的 php 代码中:
$Customer_Name = filter_input(INPUT_POST, 'Customer_Name');
html 表单中缺少一个“t”。
将 Customer_Name 替换为 html 表单中的 Cusomer_Name,并对 Cusomer_Email 和 Cusomer_Contact 做同样的事情
关于PHP & MySQL 不向表中插入数据 (PDO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35320981/