codescracker


php

PHP and MySQLi Store User Data



« Previous Tutorial Next Tutorial »


Here in this tutorial, you will learn about storing the user's data into the MySQL database using PHP and MySQLi with example.

What to do Before Storing User Data

Before storing the user data or record inside the database using PHP and MySQLi, make sure that you have already learned and created a database and a table namely codescrackerTwo and codescrackerTable.

PHP MySQLi Store User Data Example

Here is a simple example of storing the user's record inside the database using PHP and MySQLi.

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$conn = mysqli_connect('localhost', 'root', '', 'codescrackerTwo');
		if(mysqli_connect_error())
		{
			echo "<p>Error occurred..kindly try again later.</p>";
			exit();
		}
		$userid = $_POST["userid"];
		$userdata = $_POST["userdata"];
	}
?>
<html>
<head>
	<title>PHP and MySQLi Store User Data Example</title>
</head>
<body>

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$qry = "insert into codescrackerTable(userid, usercomment)
				values('$userid', '$userdata');";
		$res = $conn->query($qry);
		if($res)
		{
			echo "<p>Your data is stored inside the database successfully.</p>";
			exit();
		}
		else 
		{
			echo "<p>Error occurred, kindly try again later.</p>";
			exit();
		}
	}
?>

<form method="post">
Enter Comment:<br/>
<input type="text" name="userid" maxlength="40" placeholder="enter your username"><br/>
<textarea name="userdata" style="height:60px;" maxlength="200" placeholder="write comment"></textarea><br/>
<input type="submit" name="comment" value="Comment">
</form>

</body>
</html>

Here is the sample output produced by the above example of storing the user's data in database using PHP.

php mysqli store user data

Now enter your username and data as shown in the figure given below:

store user data in database php

Now click on the Comment button, after performing this, your data will be stored inside the database and here is the sample output that will be shown in your browser after clicking on the Comment button.

php mysqli store user record

And below is the screenshot of the table named codescrackerTable in which the user's comment is stored using the above example of storing the user data in MySQL database using PHP.

store user data php mysqli

Let's take another example to understand about storing the user's data in database using PHP and MySQLi in a better way.

<?php 
	$conn = mysqli_connect('localhost', 'root', '', 'codescrackerTwo');
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		if(mysqli_connect_error())
		{
			echo "<p>Error occurred..kindly try again later.</p>";
			exit();
		}
		$userid = $_POST["userid"];
		$userdata = $_POST["userdata"];
	}
?>
<html>
<head>
	<title>PHP and MySQLi Store User Data Example</title>
</head>
<body>

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$qry = "insert into codescrackerTable(userid, usercomment)
				values('$userid', '$userdata');";
		$res = $conn->query($qry);
		if($res)
		{
		}
		else 
		{
			echo "<p>Error occurred, kindly try again later.</p>";
			exit();
		}
	}
?>

<p>This tutorial is for storing user data in MySQL database using PHP with MySQLi.</p>
<b><i>Users Comment:</i></b><br/>
<?php 
	$qry = "select * from codescrackerTable";
	$res = $conn->query($qry);
	if($res)
	{
		while($row=mysqli_fetch_row($res))
		{
			echo $row[3];
			echo " by <b>".$row[2]."</b>";
			echo " at ".$row[1];
			echo "<br/>";
		}
	}
?>

<form method="post">
<br/>Enter Your Comment:<br/>
<input type="text" name="userid" maxlength="40" placeholder="enter your username"><br/>
<textarea name="userdata" style="height:60px;" maxlength="200" placeholder="write comment"></textarea><br/>
<input type="submit" name="comment" value="Comment">
</form>

</body>
</html>

Here is the sample output that will produce after running the above example code of PHP to store users data in MySQL database.

store user information php mysqli

Now type your userid and your data as shown in the below figure:

php mysqli store user data example

Now click on the Comment button, here is the sample output that will be shown in your browser after performing this:

store user data php example

Your data is stored in the database and you can also see that your comment is displayed in the second line of users comment section.


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test