This question already has an answer here:
How can I get my form to input the fields to my database using MySQLi (mysqli procedural) I currently have this but it goes though when I submit with no errors but nothing goes into the database.
<?php
if(isset($_POST["submit"])){
$Fname = $_POST['firstname'];
$Lname = $_POST['lastname'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$bday = $_POST['bday'];
$servername = "<--enter servername -->";
$username = "<--enter username -->";
$password = "<--enter password -->";
$database = "<--enter datbase name -->";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
$sql = "INSERT INTO Questions_users (Fname, Lname, email, gender, bday)
VALUES ('$Fname','$Lname','$email','$gender','$bday')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
And the form is
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="firstname"></td>
<input type="text" name="lastname"></td>
<input type="email" name="email"></td>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female</td>
<input type="date" name="bday"></td>
<input type="submit" value="Submit"></form>
</div>
You'll need to set the name
attribute for this:
<input type="submit" name="submit" value="Submit">
Your PHP code was not executed as you set the condition if(isset($_POST["submit"])){
and the submit
element was not found.
Also, you've missed out the closing bracket:
$sql = "INSERT INTO Questions_users (Fname, Lname, email, gender, bday)
VALUES ('[$Fname]','[$Lname]','[$email','[$gender]','[$bday]')";
'[$email'
should be '[$email]'
.