When I run this code it all goes fine. It gives me the message "New record inserted succesfully". The problem that I have is whenever I go to my databse to check it out. It's still empty, the information that I typed into the input boxes don't appear in my database. Just a blank database. I'm new to coding so please bear with me if I misused some words or don't quite understand my mistakes.
<!DOCTYPE HTML>
<html>
<head>
<title>Sign Up Form</title>
<meta charset="utf-8">
<link rel="stylesheet" href="SignUp.css" type="text/css">
</head>
<body>
<form action="insert.php" method="POST">
<table>
<tr>
<td style="color:white">Username :</td>
<td><input type="text" name="Username" required></td>
</tr>
<tr>
<td style="color:white">voornaam :</td>
<td><input type="text" name="Voornaam" required></td>
</tr>
<tr>
<td style="color:white">Achternaam :</td>
<td><input type="text" name="Achternaam" required></td>
</tr>
<tr>
<td style="color:white">Wachtwoord :</td>
<td><input type="password" name="Wachtwoord" required></td>
</tr>
<tr>
<td style="color:white">Adres :</td>
<td><input type="text" name="Adress" required></td>
</tr>
<tr>
<td style="color:white">Telefoon nummer :</td>
<td>
<select name="Telcode" required>
<option selected hidden value="">Select Code</option>
<option value="297">297</option>
</select>
<input type="phone" name="Telnummer" required>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
$Voornaam = $_POST['Voornaam'];
$Username = $_POST['Username'];
$Achternaam = $_POST['Achternaam'];
$Wachtwoord = $_POST['Wachtwoord'];
$Adress = $_POST['Adress'];
$Telcode = $_POST['Telcode'];
$Telnummer = $_POST['Telnummer'];
if (!empty($Voornaam) || !empty($Achternaam) || !empty($Wachtwoord) || !empty($Adress) || !empty($Telcode) || !empty($Telnummer) || !empty($Username))
{
$host = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbname = "gevuldetomaat";
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
} else {
$SELECT = "SELECT Username From klant Where Username = ? Limit 1";
$INSERT = "INSERT INTO klant (Wachtwoord, Achternaam, Voornaam, Telnummer, Telcode, Username, Adress) values(?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $Username);
$stmt->execute();
$stmt->bind_result($Username);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0)
{
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("sssiiss", $Wachtwoord, $Achternaam, $Voornaam, $Telnummer, $Telcode, $Username, $Adress);
$stmt->execute();
echo "New record inserted sucessfully";
}
else {
echo "Someone already registered using this name";
}
$stmt->close();
$conn->close();
}
}
else {
echo "All fields are required";
die();
}
?>
It seems like you do run into that fork where the INSERT
SQL runs, but you print the success message without checking whether it indeed was successfully inserted or not
After each mysqli_stmt::execute()
, you should consider checking the mysqli_stmt::$errno
variable to see if there was an error or not ($errno
is short for "error number", if it's zero, there were no errors)
Something like this should tell you more about the error that happens:
if ($rnum == 0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("sssiiss", $Wachtwoord, $Achternaam, $Voornaam, $Telnummer, $Telcode, $Username, $Adress);
$stmt->execute(); // Run the INSERT statement
if ($stmt->errno) { // Check for errors
die("Error in inserting: " . $stmt->error); // Print last error
} else { // No errors
echo "New record inserted sucessfully"; // Success message
}
} else {
echo "Someone already registered using this name";
}
I did this only for the INSERT
statement here, but you should consider doing something like this for the SELECT
statement too
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments