我正在使用带有预处理语句的脚本在我的数据库中创建记录,在这种情况下创建一个客户。
我想在脚本中添加一个检查,以查看查询是否成功运行。在做了一些研究之后,我遇到了this
StackOverflow帖子。 execute();
返回一个布尔值,所以我可以添加if($stmt-execute())
,这是有道理的。
我添加了以下代码,以查看我的查询是否成功运行,添加一个类并相应地显示一条消息。
if($stmt->execute()) {
$style = "class='succes'";
$msg = "De klant is toegevoegd!";
echo "<div " . $style . ">";
echo "<p>" . $msg . "</p>";
echo "</div>";
} else {
$style = "class='fail'";
$msg = "De klant kon niet worden toegevoegd, probeer het later opnieuw.";
echo "<div " . $style . ">";
echo "<p>" . $msg . "</p>";
echo "</div>";
}
}
我的总代码现在看起来像这样。
<?php
if(isset($_POST['submit'])) {
require('dbconfig.php');
$stmt = $connect->prepare('INSERT INTO `customers` (customer_name, customer_type, customer_address, customer_postal, customer_phone, customer_email, customer_company, customer_city) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$name = $_POST['customerName'];
$type = $_POST['customerType'];
$address = $_POST['customerAddress'];
$postal = $_POST['customerPostal'];
$phone = $_POST['customerPhone'];
$email = $_POST['customerEmail'];
$company = $_POST['customerCompany'];
$city = $_POST['customerCity'];
$stmt->bind_param('ssssssss', $name, $type, $address, $postal, $phone, $email, $company, $city);
$stmt->execute();
$connect->close();
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>DETACHIT - ADD CUSTOMER</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Oxygen" rel="stylesheet">
<link rel="stylesheet" href="./css/detachit-webapp.css">
<link rel="icon" href="./img/favicon.ico">
</head>
<body>
<div class="container">
<div class="top-navbar">
<ul>
<li><a href="webapp.php"><i class="fa fa-home"></i></a></li>
<li><a href="settings.php"><i class="fa fa-cogs"></i></a></li>
<li><a href="my-account.php"><i class="fa fa-user"></i></a></li>
<li><a onclick="pageBack()"><i class="fa fa-arrow-left"></i></a></li>
<li><a onclick="pageForward()"><i class="fa fa-arrow-right"></i></a></li>
<li style="float:right"><a href="logout.php">Uitloggen</a></li>
</ul>
</div>
<div class="inner-container">
<div class="left-inner-container">
<form class="webapp-form" method="post">
<h3>Klant toevoegen</h3>
<input type="text" name="customerName" placeholder="Naam" value="">
<select name="customerType">
<option value="0">Selecteer...</option>
<option value="Particulier">Particulier</option>
<option value="Bedrijf">Bedrijf</option>
<option value="Anders">Anders</option>
</select>
<input type="text" name="customerAddress" placeholder="Straat en huisnummer">
<input type="text" name="customerPostal" placeholder="Postcode">
<input type="text" name="customerPhone" placeholder="Telefoonnummer">
<input type="text" name="customerEmail" placeholder="Email adres">
<input type="text" name="customerCompany" placeholder="Bedrijfsnaam">
<input type="text" name="customerCity" placeholder="Plaats">
<input type="submit" name="submit" value="Aanmaken">
</form>
</div>
<div class="right-inner-container">
<h3>Klanten toevoegen</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<?php
if(isset($_POST['submit'])) {
if($stmt->execute()) {
$style = "class='succes'";
$msg = "De klant is toegevoegd!";
echo "<div " . $style . ">";
echo "<p>" . $msg . "</p>";
echo "</div>";
} else {
$style = "class='fail'";
$msg = "De klant kon niet worden toegevoegd, probeer het later opnieuw.";
echo "<div " . $style . ">";
echo "<p>" . $msg . "</p>";
echo "</div>";
}
}
?>
</div>
</div>
</div>
</body>
</html>
但这就是问题的起点。我测试了我的脚本并返回了错误消息。为了确保它失败了,我检查了我的数据库,但记录在那里。
为什么if($stmt->execute())
在我的查询成功运行时会返回FALSE
?
答案 0 :(得分:1)
您需要删除$stmt->execute();
,因为该语句在if语句中执行。
它现在正在做两次,这是行不通的。