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.

Dani AI

Generated

Main problems in the snippet are easy to miss but common: the connection check logic is inverted (so a successful connect triggers a "failed" path), a likely typo in the lastname variable (you assign one name but insert another), the INSERT runs regardless of whether the form was posted, and the code uses raw POST values in SQL (SQL injection risk). correctly called out the inverted connection check — that needs fixing — but there are a few more practical hardening and debugging steps to follow.

Use strict connection/error handling and set charset, then only run the insert when you actually have POST data. Example of a safer connection pattern (object style, with exceptions):

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $db = new mysqli($host, $user, $pass, $dbName);
    $db->set_charset('utf8mb4');
} catch (mysqli_sql_exception $e) {
    error_log('DB connect failed: ' . $e->getMessage());
    exit('Database unavailable');
}

Use prepared statements and validate inputs before inserting. Example flow for POST handling and insertion:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $first = trim(filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING));
    $last  = trim(filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_STRING));
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

    $stmt = $db->prepare('INSERT INTO tutorsignup (firstname, lastname, email) VALUES (?, ?, ?)');
    $stmt->bind_param('sss', $first, $last, $email);
    $stmt->execute();
}

Troubleshooting checklist: fix the lastname variable typo so the same variable is used for assignment and insert; confirm DB credentials and GRANTs for the user; check table/column names match exactly; enable mysqli exceptions during development and log errors instead of printing credentials or raw SQL; disable display_errors on production. See PHP manual on prepared statements and mysqli error reporting for details: mysqli prepared statements and .

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.