In this tutorial i will show you how to insert data into database using php and mysql. In this tutorial we will see how we can insert data to database using procedural, object oriented php and finally using pdo.
You can use whatever you want to insert data into database. Here i will create a HTML form and then using this form we will save data into database. So let's start our insert form data into database using php tutorial.
Step 1: Create HTML Form
Now create a html form. To make it paste this below code to your file.
Having done this, we have to catch this form data. Using php super global array $_POST, $_GET or $_REQUEST we can catch this form data. When a user hit the submit button then the form data will be inserted into database.
Here is the complete code of our 'insert.php' file:
Example of procedural:
$link = mysqli_connect("localhost", "root", "", "demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['last_name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
Example of object oriented:
$mysqli = new mysqli("localhost", "root", "", "demo");
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
$first_name = $mysqli->real_escape_string($_REQUEST['first_name']);
$last_name = $mysqli->real_escape_string($_REQUEST['last_name']);
$email = $mysqli->real_escape_string($_REQUEST['email']);
$sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
$mysqli->close();
Example of pdo:
try{
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
try{
$sql = "INSERT INTO persons (first_name, last_name, email) VALUES (:first_name, :last_name, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':first_name', $_REQUEST['first_name']);
$stmt->bindParam(':last_name', $_REQUEST['last_name']);
$stmt->bindParam(':email', $_REQUEST['email']);
$stmt->execute();
echo "Records inserted successfully.";
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
unset($pdo);
You can use prepare statement for better security and performance with pdo.
Note: The
mysqli_real_escape_string()
escapes special characters in a string and provide security against SQL Injection
This is very simple example of inserting the form data in a MySQL database table. You can validate this data in many ways. This is totally up to you how you will do that. I just try to show you how you can insert form data into database using mysql.
#php #mysql #insert #pdo #oop