Simple PHP CRUD Tutorial

Download Whole Scripts

Create Operation
insert.php

<?php
require("dbconfig.php");
if(count($_POST)>0)
{
	$sql="INSERT INTO usersinfo (username,password,firstname,lastname) VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["firstname"]."','".$_POST["lastname"]."') ";
	
	if(mysqli_query($connection,$sql)){
		$message = "Inserted successfully";
	}else{
		$message = "Error:".$sql."</br>".mysqli_error($connection);
	}
}
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form name="formuser" method="post" action="">

<div class="message">
<?php if(isset($message))
{
	echo $message;
}	
?>
</div>
<table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="tablesaveForm">

<tr class="tableheader">
<td colspan="2">Add new user</td>
</tr>

<tr>
<td><label>Username</label></td>
<td><input type="text" name="username" class="textFeild"></td>
</tr>

<tr>
<td><label>Password</label></td>
<td><input type="pasword" name="password" class="textFeild"></td>
</tr>
<tr>

<tr>
<td><label>First Name:</label></td>
<td><input type="text" name="firstname" class="textFeild"></td>
</tr>

<tr>
<td><label>Last Name:<label></td>
<td><input type="text" name="lastname" class="textFeild"></td>
</tr>

<tr>
<td>
<input type="submit" name="submit" value="Submit" class="buttonSubmit">
</td>
</tr>

</table>
</form>

<div class="message">
<p><a href="display.php">Read All</a></p>
<p>&copy; By Zaki Live 2016</p>
</div>

</body>

</html>
insert.php
insert.php

Read Operation
display.php

<?php
require("dbconfig.php");

	$sql="SELECT * FROM usersinfo";
	
	$result=mysqli_query($connection,$sql);
?>





<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form name="formuser" method="post" action="">

<div class="message">
<?php if(isset($message))
{
	echo $message;
}	
?>
</div>
<table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="tablesaveForm">

<tr class="listheader">
<td>Username</td>
<td>Password</td>
<td>First Name</td>
<td>Last Name</td>
<td>CRUD Actions</td>
</tr>

<?php
$i=0;
while($row=mysqli_fetch_array($result,MYSQLI_BOTH)){
	//$userid=$row['userid'];

	if($i%2==0)
		$classname="evenRow";
	else
		$classname="oddRow";

?>


<tr class="<?php if(isset($classname)) echo $classname; ?>">
<td><?php echo $row["username"]; ?></td>
<td><?php echo $row["password"]; ?></td>
<td><?php echo $row["firstname"]; ?></td>
<td><?php echo $row["lastname"]; ?></td>
<td><a href="edit.php?userid=<?php echo $id=$row['id']; ?>&username=<?php echo $id=$row['username']; ?>&password=<?php echo $id=$row['password']; ?>&firstname=<?php echo $id=$row['firstname']; ?>&lastname=<?php echo $id=$row['lastname']; ?>" class="link"><img alt='Edit' title='Edit' src='edit.png' width='15px' height='15px' hspace='10' /></a> <a href="delete.php?userid=<?php echo $id=$row['id']; ?>&username=<?php echo $id=$row['username']; ?>&password=<?php echo $id=$row['password']; ?>&firstname=<?php echo $id=$row['firstname']; ?>&lastname=<?php echo $id=$row['lastname']; ?>" class="link"><img alt='Edit' title='Edit' src='delete.png' width='15px' height='15px' hspace='10' /></a></td>
</tr>

<?php
$i++;
}
?>


</table>
</form>

<div class="message">
<p><a href="insert.php">Insert Again</a></p>
<p>&copy; By Zaki Live 2016</p>
</div>


</body>

</html>

 

display
UPDATE operation

edit.php

<?php
require("dbconfig.php");

$id0 =$_REQUEST['userid'];
$id1 =$_REQUEST['username'];
$id2 = $_REQUEST['password'];
$id3 =$_REQUEST['firstname'];
$id4=$_REQUEST['lastname'];


$result1 = mysqli_query($connection,"SELECT * FROM usersinfo WHERE username 	= '$id1'");
$result2 = mysqli_query($connection,"SELECT * FROM usersinfo WHERE password  	= '$id2'");
$result3 = mysqli_query($connection,"SELECT * FROM usersinfo WHERE firstname  = '$id3'");
$result4 = mysqli_query($connection,"SELECT * FROM usersinfo WHERE lastname	= '$id4'");


$test1 = mysqli_fetch_array($result1);
$test2 = mysqli_fetch_array($result2);
$test3 = mysqli_fetch_array($result3);
$test4 = mysqli_fetch_array($result4);



$username= $test1['username'] ;
$password= $test2['password'] ;
$firstname= $test3['firstname'];
$lastname=$test4['lastname'];


if(isset($_POST['submit']))
{	
	$username= $_POST['username'];
	$password= $_POST['password'];
	$firstname=$_POST['firstname'] ;
	$lastname=$_POST['lastname'];

	
	mysqli_query($connection,"UPDATE usersinfo SET username ='$username',password='$password',firstname='$firstname',lastname='$lastname' WHERE id='$id0'")
				or die(mysqli_error($connection)); 	
}




mysqli_close($connection);

?>





<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form name="formuser" method="post" action="">

<div class="message">
<?php if(isset($message))
{
	echo $message;
}	
?>
</div>
<table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="tablesaveForm">

<tr class="tableheader">
<td colspan="2">Add new user</td>
</tr>
<?


?>
<tr>
<td><label>UserID</label></td>
<td><input type="text" name="id" class="textFeild" value="<?php echo $id0; ?>"></td>
</tr>

<tr>
<td><label>Username</label></td>
<td><input type="text" name="username" class="textFeild" value="<?php echo $username; ?>"></td>
</tr>

<tr>
<td><label>Password</label></td>
<td><input type="pasword" name="password" class="textFeild" value="<?php echo $password; ?>"></td>
</tr>
<tr>

<tr>
<td><label>First Name:</label></td>
<td><input type="text" name="firstname" class="textFeild" value="<?php echo $firstname; ?>"></td>
</tr>

<tr>
<td><label>Last Name:<label></td>
<td><input type="text" name="lastname" class="textFeild" value="<?php echo $lastname; ?>"></td>
</tr>

<tr>
<td>
<input type="submit" name="submit" value="submit" class="buttonSubmit">
</td>
</tr>

</table>
</form>

<div class="message">
<p><a href="display.php">Read All</a></p>
<p><a href="insert.php">Insert Again</a></p>
<p>&copy; By Zaki Live 2016</p>
</div>


</body>

</html>

edit

Delete Operation

delete.php

<?php
  include("dbconfig.php");  

$id0 =$_REQUEST['userid'];
$id1 =$_REQUEST['username'];
$id2 = $_REQUEST['password'];
$id3 =$_REQUEST['firstname'];
$id4=$_REQUEST['lastname'];
	
	
	
	// sending query
	mysqli_query($connection,"DELETE FROM usersinfo WHERE id = '$id0'")
	or die(mysqli_error($connection));  	
	
	header("Location: display.php");
?>
One row deleted
One row deleted
It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.