Reputation: 2454
I am using a script with prepared statements to create a record in my database, create a customer in this case.
I wanted to add a check to the script to see whether the query ran succesfully or not. After doing some research I came across this
StackOverflow post. execute();
returns a boolean, so I can just add if($stmt-execute())
, makes sense.
I added below code to see if my query ran succesfully, add a class and display a message accordingly.
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>";
}
}
My total code now looks like this.
<?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>
But this is where the problem started. I tested my script and it returned the error message. To make sure that it had failed, I checked my database, but the record was there.
Why does if($stmt->execute())
return FALSE
when my query ran succesfully?
Upvotes: 1
Views: 122
Reputation: 26
You need to remove $stmt->execute();
because the statement gets executed in the if-statement.
It's now doing it twice, which will not work.
Upvotes: 1