Я использую сценарий с подготовленными утверждениями для создания записи в моей базе данных, в этом случае создайте клиента.
Я хотел добавить проверку в скрипт, чтобы увидеть, успешно ли выполняется запрос. После некоторых исследований я наткнулся на это
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
, когда мой запрос был успешно выполнен?