Google Ads

Easy and Simple Adding/Inserting Data into MySQL Database PHP Script


Easy and Simple Adding/Inserting Data into MySQL Database PHP Script

This tutorial will show you an easy and simple way in adding/inserting data into mysql database using 3 methods which are MySQLi Object-oriented, MySQLi Procedural and PDO. This tutorial does not include a good design but will give you knowledge in adding/inserting data into mysql database using the 3 methods.
Creating our Database

First, we’re going to create a database that contains our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “add”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

CREATE TABLE `user` (
`userid` INT(11) NOT NULL,
`firstname` VARCHAR(30) NOT NULL AUTO_INCREMENT,
`lastname` VARCHAR(30) NOT NULL,
`added_via` VARCHAR(20) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

 

Creating our Add form and Add Script using the 3 methods

Lastly, we create our add form that will add the data into our database. We name this page as “index.php”. We also include the 3 methods of adding into this page. To create the page, open your HTML code editor and paste the code below after the tag.

 

<!DOCTYPE html>
<html>
<head>
<title>Adding/Inserting Data into MySQL</title>
</head>
<body>
<div>
<h2>Add Form</h2>
<form method="POST">
<label>Firstname: <input type="text" name="firstname"></label>
<label>Lastname: <input type="text" name="lastname"></label>
</br></br>
<input type="submit" value="MySQLi Object-oriented" name="mysqli_oop">
<input type="submit" value="MySQLi Procedural" name="mysqli_procedural">
<input type="submit" value="PDO" name="pdo">
</form>

<?php
//MySQLi Object-oriented
if (isset($_POST['mysqli_oop'])){

// Create connection
$con = new mysqli("localhost", "root", "", "add");
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

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

$sql = "INSERT INTO `user` (firstname, lastname, added_via)
VALUES ('$firstname', '$lastname', 'MySQLi Object-oriented')";

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

$con->close();
}
//MySQLi Procedural
elseif (isset($_POST['mysqli_procedural'])){

// Create connection
$con = mysqli_connect("localhost", "root", "", "add");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

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

$sql = "INSERT INTO `user` (firstname, lastname, added_via)
VALUES ('$firstname', '$lastname', 'MySQLi Procedural')";

if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

mysqli_close($con);
}
//PDO
elseif (isset($_POST['pdo'])){

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "add";

try {
$con = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

$sql = "INSERT INTO `user` (firstname, lastname, added_via)
VALUES ('$firstname', '$lastname', 'PDO')";
// use exec() because no results are returned
$con->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$con = null;
}
?>

</div>
<br>
<div>
<table border="1">
<thead>
<th>UserID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Added via</th>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost","root","","add");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query=mysqli_query($con,"select * from `user`");
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['userid']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['added_via']; ?></td>
</tr>
<?php
}

?>
</tbody>
</table>
</div>
</body>
</html>

 

 


Related Post

SiteSpy v4.1 – The Most Complete Visitor Ana... SiteSpy v4.1 - The Most Complete Visitor Analytics & SEO Tools PHP Script The Most Complete Visitor Analytics & SEO package including visit...
B2Evolution v6.8.6 Stable Blog PHP Script B2Evolution v6.8.6 Stable Blog PHP Script Create a beautiful blog website using this php scrpt. b2evolution is an advanced weblog tool, i-e soft...
WeBid v0.8.1 Auction PHP Script Download WeBid v0.8.1 Auction PHP Script Download WeBid is an open-source auction script written in the popular language PHP which a massive collection of i...
OVOO v2.5.5 – Movie & Video Streaming C... OVOO v2.5.5 - Movie & Video Streaming CMS with Unlimited TV-Series - nulled PHP Script OVOO is a powerful, flexible and User friendly movie &am...
Comment Box Tagboard PHP Script Comment Box Tagboard PHP Script A shoutbox is a box that can be placed on your website where your visitors can leave their comments directly. It i...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *