准备好的语句返回 false 但查询成功
Prepared statement returning false but query was successful
我正在使用带有准备好的语句的脚本在我的数据库中创建记录,在这种情况下创建客户。
我想在脚本中添加一个检查以查看查询 运行 是否成功。做了一些研究后,我遇到了 this
Whosebug post。 execute();
return 是一个布尔值,所以我可以只添加 if($stmt-execute())
,这是有道理的。
我添加了下面的代码来查看我的查询 运行 是否成功,添加一个 class 并相应地显示一条消息。
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>
但这就是问题开始的地方。我测试了我的脚本,它 return 编辑了错误消息。为了确保它失败了,我检查了我的数据库,但记录在那里。
为什么 if($stmt->execute())
return FALSE
我的查询 运行 成功了?
您需要删除 $stmt->execute();
,因为该语句在 if 语句中执行。
现在做了两次,不行。
我正在使用带有准备好的语句的脚本在我的数据库中创建记录,在这种情况下创建客户。
我想在脚本中添加一个检查以查看查询 运行 是否成功。做了一些研究后,我遇到了 this
Whosebug post。 execute();
return 是一个布尔值,所以我可以只添加 if($stmt-execute())
,这是有道理的。
我添加了下面的代码来查看我的查询 运行 是否成功,添加一个 class 并相应地显示一条消息。
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>
但这就是问题开始的地方。我测试了我的脚本,它 return 编辑了错误消息。为了确保它失败了,我检查了我的数据库,但记录在那里。
为什么 if($stmt->execute())
return FALSE
我的查询 运行 成功了?
您需要删除 $stmt->execute();
,因为该语句在 if 语句中执行。
现在做了两次,不行。