Site URL
https://tamato.infinityfree.io
Inputting Data to MySQL!
I made a new database just to test in in MySQL. I called it class registration, my intent is to capture the student’s first name, last name, and email address to enter into my SQL database.

I just made a new form on my site using my existing form structure that I have, and renamed the text boxes to fit the information that I wanted to capture.

when the user types in their information, I have if statements checking if the fields are blank. If they are, it will send an error to the user, letting them know to fill out all the fields, as they are all required. I am also using a built in php function to validate that the email is a real email address. The code is below:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { exit('Please enter a valid email address.');}
Once the user clicks submit. I run the input through my insert.php script. Below is the code.
$firstName = trim($_POST['firstName'] ?? '');$lastName = trim($_POST['lastName'] ?? '');$email = trim($_POST['email'] ?? '');if ($firstName === '' || $lastName === '' || $email === '') { exit('First Name, Last Name, and email address are required.');}if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { exit('Please enter a valid email address.');}// Create connection $conn = new mysqli($servername, $username, $password, $dbname); $conn->set_charset('utf8mb4'); $stmt = $conn->prepare('INSERT INTO CLASS_REGISTRATION (firstName, lastName, email) VALUES (?, ?, ?)'); $stmt->bind_param('sss',$firstName, $lastName, $email); $stmt->execute(); $stmt->close(); $conn->close(); header("Location: success_2.html", true, 303); exit;?>
Here I am getting the information from the form, then checking to make sure the information is valid. They I connect to my database (I did not include the variables for my database here). I am preparing the SQL statement for the database, then bind the information in the following step. I then execute the statement, then close the statement, then close the database. If successful, I send the user to my success_2.html page that gives the user confirmation that their information has been submitted successfully, it allows them to return to the form or to the main page.
Here is a screenshot of my test data:

This is a screenshot of my success_2.html page

Leave a Reply