codescracker


php

PHP and MySQLi Insert Record into Table



« Previous Tutorial Next Tutorial »


Here you will learn about how to insert data or record into MySQL table using PHP with MySQLi.

What to do Before Inserting Record into Table

Before inserting data into the table, be sure that you have already learned the previous tutorial and created a database and a table namely codescrackerTwo and codescrackerTable (with 4 columns).

PHP and MySQLi Insert Record Example

Here is an example demonstrates about inserting new data or record into the MySQL table using PHP with MySQLi.

<?php 
	$serverName = "localhost";
	// as the default server name is localhost
	$username = "root";
	// as the default username is root
	$password = "";
	// as the default password is blank
	$databaseName = "codescrackerTwo";
	// the created database name is codescrackerTwo
	$conn = mysqli_connect($serverName, $username, $password, $databaseName);
	if(mysqli_connect_error())
	{
		echo "<p>There is an error occurred during the database connection.</p>";
		echo "<p>Please try again after some time</p>";
		echo "<p>Exiting....</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Insert Data into Table Example</title>
</head>
<body>
<?php
	$tableName = "codescrackerTable";
	$sqlQuery = "insert into $tableName(userid, usercomment)
					values('codescracker@gmail.com', 'I just love to learning PHP and MySQLi
					Tutorial here with examples.');";
	$retRes = $conn->query($sqlQuery);
	if($retRes)
	{
		echo "<p>Data successfully inserted into the table.</p>";
	}
	else 
	{
		echo "<p>Some error occurred during inserting the data into 
				the table...exiting...</p>";
		exit();
	}
?>
</body>
</html>

Here is the sample output produced by the above example of PHP and MySQLi to insert data or record into the table:

php mysqli insert data into table

After performing the above example, you will see into your database table named codescrackerTable, the above record will be added into that table. Here is the screenshot of the table after performing or running the above example code in your browser:

php mysqli insert record into table

PHP and MySQLi Insert Automated Record into Table

Here is a sample example used to insert automated and continuous record or data into the MySQL table using PHP and MySQLi.

<?php 
	$serverName = "localhost";
	$username = "root";
	$password = "";
	$databaseName = "codescrackerTwo";
	$conn = mysqli_connect($serverName, $username, $password, $databaseName);
	if(mysqli_connect_error())
	{
		echo "<p>There is an error occurred during the database connection.</p>";
		echo "<p>Please try again after some time</p>";
		echo "<p>Exiting....</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Insert Data into Table Example</title>
</head>
<body>
<?php
	$tableName = "codescrackerTable";
	for($i=2; $i<=10; $i++)
	{
		$sqlQuery = "insert into $tableName(userid, usercomment)
					values('codescracker@gmail.com', 'Comment Number: $i');";
		$retRes = $conn->query($sqlQuery);
		if($retRes)
		{
			echo "<p>Data successfully inserted into the table.</p>";
		}
		else 
		{
			echo "<p>Some error occurred during inserting the data into 
					the table...exiting...</p>";
			exit();
		}
	}
?>
</body>
</html>

Here is the sample output produced by the above PHP and MySQLi inserting automated and continuous data or record into the MySQL table example code.

php mysqli insert automated record

And below is the screenshot of the table where you have inserted the above automated data.

insert record into table php mysqli

As you can see that the 9 continous records are inserted into the table using the above example.

You can insert any amount of data into the table using loop as shown in the above example code.

Below is the optimized and short version of the above example code of PHP and MySQLi used to insert automated record.

<?php 
	$conn = mysqli_connect('localhost', 'root', '', 'codescrackerTwo');
	if(mysqli_connect_error())
	{
		echo "<p>Error occurred..exiting..</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Inserting Automatic Record Example</title>
</head>
<body>
<?php
	for($i=11; $i<=20; $i++)
	{
		$sqlQuery = "insert into codescrackerTable(userid, usercomment)
					values('codescracker@gmail.com', 'Comment Number: $i');";
		$retRes = $conn->query($sqlQuery);
		if(!$retRes)
		{
			echo "<p>Error...exiting...</p>";
			exit();
		}
	}
	$tot = $i--;
	if($tot>1)
	{
		echo "<p>All the records are inserted successfully</p>";
	}
	else 
	{
		echo "<p>Record inserted successfully</p>";
	}
?>
</body>
</html>

Below is the sample output produced by the above example of PHP and MySQLi of inserting record.

insert data into table php mysqli

And here is the screenshot after running the above example of inserting the new records.

inserting new record into table php example

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test