PHP Connects, Insert, Display, Delete and Update
Welcome to PHP Tutorial
How to connect to MySql with PHP in Xampp
- Install Xampp
- Start Apache, MySql
- Type localhost on Chrome browser or any browser on your PC
- Goto phpmyadmin and create a database
- name your database eg: 'testdb'
- create a php file with the php extension eg: 'connect.php'
- Type in the Following Code:
<?php
$user = 'root';
$pass = '';
$db = 'testdb';
$db = new mysqli('localhost', $user, $pass, $db) or die ("Unable to connect");
echo "Great Work!!";
?>
- Save the file under the Xamp/htdocs directory as 'connect.php'
- After that run the code by type in the Chrome browser as 'localhost/connect.php' and hit enter.
- You'll see the output as 'Great Work!!' which means it is successfully connected to your database.
References
How to Insert Form Data Into MySql Database Using PHP
- Create a database name as 'tutorial' and click on Go
- Create a table name as 'person'
- Create three columns name as 1) 'ID' datatype:Int, Auto_Increment: Checked 2) 'Name' datatype:Varchar, Length: 20 and 3) 'Email' datatype:Varchar, Length: 20 then click on Save.
- Create a html form name type in the following code
<!DOCTYPE html>
<html>
<head>
<title>Insert Form Data In MySql Database Using PHP</title>
</head>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="username">
<br/>
Email: <input type="text" name="email">
<br/>
<input type="submit" value="Insert">
</form>
</body>
</html>
- Now save the html code as 'index.html'
- Create a php file in the following code
<?php
$con = mysqli_connect('localhost', 'root','');
if (!$con)
{
echo 'Not Connected To Server';
}
if (!mysqli_select_db($con,'tutorial'))
{
echo 'Database Not Selected';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name, Email) VALUES ('$Name', '$Email')";
if(!mysqli_query($con, $sql))
{
echo 'Not Inserted!';
}
else
{
echo 'Inserted';
}
header ("refresh:2; url=index.php");
?>
- Save the php code as 'insert.php'
- run the php code in your chrome browser.
- it will automatically refresh after 2 second after you have inserted.
References
How to Display Records From MySql Database Using PHP
- create a file in the following code below:
<!DOCTYPE html>
<html>
<head>
<title>How to Display Data from MySql Database Using PHP</title>
</head>
<body>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<?php
//Create Connection With MySql Database
$con = mysqli_connect('localhost', 'root','');
//Select Database
if(!mysqli_select_db($con,'tutorial'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM person";
//execute the SQL query
$records = mysqli_query ($con, $sql);
while ($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Email']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
- After that save the php code as index.php
- then run the program by typing 'localhost/index.php'
- Note: make sure that you enable Xampp: Mysql and Apache should turn on
References
How to delete records from MySql Database Using PHP
- Type the following Code:
<!DOCTYPE html>
<html>
<head>
<title>Delete Data from MySql Database Using PHP</title>
</head>
<body>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Delete</th>
</tr>
<?php
//Create Connection With MySql Database
$con = mysqli_connect('localhost', 'root','');
//Select Database
if(!mysqli_select_db($con,'tutorial'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM person";
//execute the SQL query
$records = mysqli_query ($con, $sql);
while ($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Email']."</td>";
echo "<td><a href=delete.php?id=".$row['ID'].">Delete</a></td>";
}
?>
</table>
</body>
</html>
- Save as index.php
- Type the following code:
<?php
$con = mysqli_connect('localhost', 'root','');
if (!$con)
{
echo 'Not Connected To Server';
}
if (!mysqli_select_db($con,'tutorial'))
{
echo 'Database Not Selected';
}
//select Database
$sql = "DELETE FROM person WHERE ID='$_GET[id]'";
//Execute the query
if(mysqli_query ($con, $sql))
header("refresh:1; url=index.php");
else
echo "Not Deleted!";
?>
- Save as delete.php
- run your code on your browser same process as insert or display
Reference
How to Update Records in MySql Database using PHP
<!DOCTYPE html>
<html>
<head>
<title>How to Update Data In MySql Database Using PHP</title>
</head>
<body>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Email</th>
<th>Update</th>
</tr>
<?php
//Create Connection With MySql Database
$con = mysqli_connect('localhost', 'root','');
//Select Database
if(!mysqli_select_db($con,'tutorial'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM person";
//execute the SQL query
$records = mysqli_query ($con, $sql);
while ($row = mysqli_fetch_array($records))
{
echo "<tr><form action=update.php method=post>";
echo "<td><input type=text name=pname value='".$row['Name']."'></td>";
echo "<td><input type=text name=pemail value='".$row['Email']."'></td>";
echo "<input type=hidden name=id value='".$row['ID']."'>";
echo "<td><input type=submit>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>
update.php
<?php
$con = mysqli_connect('localhost', 'root','');
if (!$con)
{
echo 'Not Connected To Server';
}
if (!mysqli_select_db($con,'tutorial'))
{
echo 'Database Not Selected';
}
//Update Query
$sql = "UPDATE person SET Name='$_POST[pname]',Email='$_POST[pemail]' WHERE ID=$_POST[id]";
//Execute the query
if (mysqli_query($con,$sql))
header("refresh: 1; url=index.php");
else
echo "Not Updated!";
?>
Reference
Comments
Post a Comment