Registration using prepared statement

Loading

At first create database and table:

Database name is: student_db
table name: students

Fields of "students" table are as follows:
1) id
2) first_name
3) last_name
4) email

Note: "id" should be primary key and auto increment.

Finally add the below code

<?php

// First of all connect to database
$link = mysqli_connect("localhost", "root", "", "student_db");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Also check form is submitted or not, because we have to keep track
if(isset($_POST['submit'])){
$sql = "INSERT INTO students (first_name, last_name, email) VALUES (?, ?, ?)";

if($stmt = mysqli_prepare($link, $sql)){

mysqli_stmt_bind_param($stmt, "sss", $first_name, $last_name, $email);

// Assign first name
$first_name = $_POST['firstname'];

// similarly assign last name and email
$last_name = $_POST['lastname'];
$email = $_POST['email'];

// Finally execute query
mysqli_stmt_execute($stmt);
echo "Data inserted successfully.";
} else{
echo mysqli_error($link);
}
mysqli_stmt_close($stmt);
mysqli_close($link);
}

?>

<form name="frm" method="post">

First Name: <input type="text" name="firstname"><br/>
Last Name: <input type="text" name="lastname"><br/>
Email: <input type="text" name="email"><br/>
<input type="submit" name="submit" value="submit"><br/>
</form>