Hello, kindly look at my php code to insert registration data into a MySQL database. Here is the code below and further below, the error response I keep getting.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$servername = "localhost";
$username = "emiola";
$password = "emmybaba2020";
$dbname = "tutors";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn) {
  die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit']))
{

$title = $_POST['title'];

$firstname = $_POST['firstname'];
$flastname = $_POST['lastname'];
$email = $_POST['email'];

}

$sql = "INSERT INTO tutorsignup (firstname, lastname, email)
VALUES ('$firstname', '$lastname', '$email')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

Error Message
connect_error); } if (isset($_POST['submit'])) { $title = $_POST['title']; $firstname = $_POST['firstname']; $flastname = $_POST['lastname']; $email = $_POST['email']; } $sql = "INSERT INTO tutorsignup (firstname, lastname, email) VALUES ('$firstname', '$lastname', '$email')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . $conn->error; } $conn->close(); ?>

Thank you.

This actually not an error, the code behaves exactly as you asked it to by telling it that if your connection object is true (connected), return die and echo the error in this line of code -

if ($conn) {
  die("Connection failed: " . $conn->connect_error);
}

You should change it to NOT by using ! as in so -

if (!$conn) {
  die("Connection failed: " . $conn->connect_error);
}

//or a more correct way -

if ($conn == false) {
  die("Connection failed: " . $conn->connect_error);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.