Mysql Mysqli php Programming

PHP MySQLi CRUD Insert Update Delete

PHP MySQLi CRUD Insert Update Delete

Today, We want to share with you PHP MySQLi CRUD Insert Update Delete.
In this post we will show you PHP CRUD Operations with MySQLi Extension, hear for Multiple Insert, Update, Delete example using PHP & MySQLi we will give you demo and example for implement.
In this post, we will learn about Simple Add, Edit, Delete, View (CRUD) in PHP & MySQLi with an example.

Welcome to the In Pakainfo.com website! You will Step By Step learn web programming, easy and very fun. This website allmost provides you with a complete web programming tutorial presented in an easy-to-follow manner. Each web programming tutorial has all the practical examples with web programming script and screenshots available.

testdb.php – create Database / Table / insert records

--
-- Table structure for table `salestbl_users`
--

CREATE TABLE IF NOT EXISTS `salestbl_users` (
  `SalesuserID` int(11) NOT NULL AUTO_INCREMENT,
  `SalesuserName` varchar(20) NOT NULL,
  `SalespersonUser` varchar(50) NOT NULL,
  `salesuserimg` varchar(200) NOT NULL,
  PRIMARY KEY (`SalesuserID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

--
-- Dumping data for table `salestbl_users`
--

INSERT INTO `salestbl_users` (`SalesuserID`, `SalesuserName`, `SalespersonUser`, `salesuserimg`) VALUES
(78, 'dave chirag', 'actor, wrestler', '989855.jpg'),
(79, 'mayur dhameliya', 'singer', '98256.jpg'),
(80, 'hitesh savaliya', 'actor', '06214.jpg'),
(81, 'vishal pandya', 'wrestler', '90994.jpg');

dbconfig.php

$DB_HOST_DATA = 'localhost or ip address';
$DB_USER_DATA = 'root';
$DB_PASS_DATA = '4****125';
$DB_PASS_DATA = 'testdb';
	try{
		$Con_DB_PASS = new PDO("mysql:host={$DB_HOST_DATA};dbname={$DB_PASS_DATA}",$DB_USER_DATA,$DB_PASS_DATA);
		$Con_DB_PASS->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}
	catch(PDOException $e){
		echo $e->getMessage();
	}

index.php

	require_once 'dbconfig.php';
	if(isset($_GET['user_delete_id']))
	{
		// select Picture or Picture or Image from db to delete
		$query_stmt_select_r = $Con_DB_PASS->prepare('SELECT salesuserimg FROM salestbl_users WHERE SalesuserID =:uid');
		$query_stmt_select_r->execute(array(':uid'=>$_GET['user_delete_id']));
		$picimgRowdata=$query_stmt_select_r->fetch(PDO::FETCH_ASSOC);
		unlink("user_Picture or Images/".$picimgRowdata['salesuserimg']);
		
		// it will delete record an actual record from db
		$sales_stmt_delete_r = $Con_DB_PASS->prepare('DELETE FROM salestbl_users WHERE SalesuserID =:uid');
		$sales_stmt_delete_r->bindParam(':uid',$_GET['user_delete_id']);
		$sales_stmt_delete_r->execute();
		//redirects url
		header("Location: index.php");
	}

<html>
<head>
<title>Upload data, Insert data, Update data, Delete data an Picture or Picture or Image using PHP and MySQL - Coding script</title>
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap.min.css">
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
            <a class="navbar-brand" href="https://pakainfo.com/" title='Programming Blog'>Coding Cage</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/CRUD">CRUD</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/PDO">PDO</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/jQuery">jQuery</a>
        </div>
    </div>
</div>
<div class="container">
<div class="page-header">
<h1 class="h2">Data all members. / <a class="btn btn-default" href="addnew.php"> <span class="glyphicon glyphicon-plus"></span>   Create add new </a></h1>

    </div>
<div class="row">
<?php $stmt = $Con_DB_PASS->prepare('SELECT SalesuserID, SalesuserName, SalespersonUser, salesuserimg FROM salestbl_users ORDER BY SalesuserID DESC');
	$stmt->execute();
	
	if($stmt->rowCount() > 0)
	{
		while($row=$stmt->fetch(PDO::FETCH_ASSOC))
		{
			extract($row);
			?>

<div class="col-xs-3">


<?php echo $SalesuserName." / ".$SalespersonUser; ?>

				<img src="user_Picture or Images/<?php echo $row['salesuserimg']; ?>" class="img-rounded" width="250px" height="250px" />


				<span>
				<a class="btn btn-info" href="editform.php?data_edit_id_r=<?php echo $row['SalesuserID']; ?>" title="click for edit" onclick="return confirm('sure to edit ?')"><span class="glyphicon glyphicon-edit"></span> Edit</a> 
				<a class="btn btn-danger" href="?user_delete_id=<?php echo $row['SalesuserID']; ?>" title="click for delete" onclick="return confirm('sure to delete ?')"><span class="glyphicon glyphicon-remove-circle"></span> Delete</a>
				</span>
			</div>

			<?php } } else { ?>

<div class="col-xs-12">

<div class="alert alert-warning">
            	<span class="glyphicon glyphicon-info-sign"></span>   No Data Found ...
            </div>

        </div>

        <?php } ?>
</div>

	
<div class="alert alert-info">
    <strong>tutorial link here !</strong> <a href="https://pakainfo.com/2016/02/upload-insert-update-delete-Picture or Image-using.html">Coding Cage</a>!
</div>

</div>

<!-- New Latest compiled js and minified JavaScript(js) -->
<img src="data:image/gif;" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
</body>
</html>

addnew.php

[/php]
error_reporting( ~E_NOTICE ); // error avoid notice

Also Read :  PHP OOPS - MySQL connecting Insert update delete select Fetch Data

require_once ‘dbconfig.php’;

if(isset($_POST[‘btnsave’]))
{
$SalesuserName = $_POST[‘sl_user_name’];// user name
$userjob = $_POST[‘title_user_job’];// user email

$imgFile = $_FILES[‘user_Picture or Image’][‘name’];
$tmp_dir = $_FILES[‘user_Picture or Image’][‘tmp_name’];
$totalimgSize = $_FILES[‘user_Picture or Image’][‘size’];

if(empty($SalesuserName)){
$Message_error = “Please Write your SalesuserName.”;
}
else if(empty($userjob)){
$Message_error = “Please Write your Your Job Work.”;
}
else if(empty($imgFile)){
$Message_error = “Please Select Picture or Image File.”;
}
else
{
$name_upload_dir_n = ‘user_Picture or Images/’; // upload directory

$imgExt_sto = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get Picture or Picture or Image extension

// valid Picture or Image extensions
$data_valid_extensions = array(‘jpeg’, ‘jpg’, ‘png’, ‘gif’); // valid extensions

// rename uploading Picture or Picture or Image
$salesuserimg = rand(1000,1000000).”.”.$imgExt_sto;

// allow valid Picture or Picture or Image file formats
if(in_array($imgExt_sto, $data_valid_extensions)){
// Check file size ‘5MB’
if($totalimgSize < 5000000) { move_uploaded_file($tmp_dir,$name_upload_dir_n.$salesuserimg); } else{ $Message_error = “Sorry, your file is too large.”; } } else{ $Message_error = “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”; } } // if no error occured, continue …. if(!isset($Message_error)) { $stmt = $Con_DB_PASS->prepare(‘INSERT INTO salestbl_users(SalesuserName,SalespersonUser,salesuserimg) VALUES(:uname, :ujob, :upic)’);
$stmt->bindParam(‘:uname’,$SalesuserName);
$stmt->bindParam(‘:ujob’,$userjob);
$stmt->bindParam(‘:upic’,$salesuserimg);

Also Read :  PHP crud Mysqli Select Insert Update and Delete query

if($stmt->execute())
{
$MessageSuccess_var = “new record succesfully inserted …”;
header(“refresh:5;index.php”); // redirects Picture or Picture or Image view page after 5 seconds.
}
else
{
$Message_error = “error while inserting….”;
}
}
}

[/php]

<html>
<head>
<title>Upload data,data Insert, data Update, data Delete an Picture or Picture or Image using PHP with MySQL - Coding script</title>
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap.min.css">
<!-- it is linking - Optional - data theme link -->
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
            <a class="navbar-brand" href="https://pakainfo.com/" title='Programming Blog'>Coding Cage</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/CRUD">CRUD</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/PDO">PDO</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/jQuery">jQuery</a>
        </div>

 
    </div>

</div>
<div class="container">
<div class="page-header">
<h1 class="h2">add new user. <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-eye-open"></span>   view all </a></h1>
    </div>
	<?php if(isset($Message_error)){ ?>

<div class="alert alert-danger">
            	<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $Message_error; ?></strong>
            </div>

            <?php } else if(isset($MessageSuccess_var)){ ?>

<div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $MessageSuccess_var; ?></strong>
        </div>

        <?php } ?>   

<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>

<td><label class="control-label">SalesuserName.</label></td>
<td><input class="form-control" type="text" name="sl_user_name" placeholder="Write your SalesuserName" value="<?php echo $SalesuserName; ?>" /></td>

    </tr>
<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="title_user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>

    </tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_Picture or Picture or Image" accept="Picture or Image/*" /></td>
    </tr>
<tr>

<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
        <span class="glyphicon glyphicon-save"></span>   Data save
        </button>
        </td>

    </tr>

    </table>

</form>
<div class="alert alert-info">
    <strong>tutorial link !</strong> <a href="https://pakainfo.com/2016/02/upload-insert-update-delete-Picture or Image-using.html">Coding Cage</a>!
</div>

</div>

<!-- New Latest compiled js and minified JavaScript(js) -->
<img src="data:image/gif" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
</body>
</html>

editform.php

	error_reporting( ~E_NOTICE );
	
	require_once 'dbconfig.php';
	
	if(isset($_GET['data_edit_id_r']) && !empty($_GET['data_edit_id_r']))
	{
		$id = $_GET['data_edit_id_r'];
		$data_stmt_edit_res = $Con_DB_PASS->prepare('SELECT SalesuserName, SalespersonUser, salesuserimg FROM salestbl_users WHERE SalesuserID =:uid');
		$data_stmt_edit_res->execute(array(':uid'=>$id));
		$row_edit_result = $data_stmt_edit_res->fetch(PDO::FETCH_ASSOC);
		extract($row_edit_result);
	}
	else
	{
		header("Location: index.php");
	}
	
	
	
	if(isset($_POST['save_bttn_update_data']))
	{
		$SalesuserName = $_POST['sl_user_name'];// user name
		$userjob = $_POST['title_user_job'];// user email
			
		$imgFile = $_FILES['user_Picture or Image name']['name'];
		$tmp_dir = $_FILES['user_Picture or Image name']['tmp_name'];
		$totalimgSize = $_FILES['user_Picture or Image name']['size'];
					
		if($imgFile)
		{
			$name_upload_dir_n = 'user_Picture or Images/'; // upload directory	
			$imgExt_sto = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get Picture or Picture or Image extension
			$data_valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
			$salesuserimg = rand(1000,1000000).".".$imgExt_sto;
			if(in_array($imgExt_sto, $data_valid_extensions))
			{			
				if($totalimgSize < 5000000) { unlink($name_upload_dir_n.$row_edit_result['salesuserimg']); move_uploaded_file($tmp_dir,$name_upload_dir_n.$salesuserimg); } else { $Message_error = "Sorry, your file is too large it should be less then 5MB"; } } else { $Message_error = "Sorry, only JPG img, JPEG img, PNG img & GIF img files or images are allowed."; } } else { // if no Picture or Image selected the old Picture or Picture or Image remain as it is. $salesuserimg = $row_edit_result['salesuserimg']; // old Picture or Picture or Image from database } // if no error occured, continue .... if(!isset($Message_error)) { $stmt = $Con_DB_PASS->prepare('UPDATE salestbl_users 
									     SET SalesuserName=:uname, 
										     SalespersonUser=:ujob, 
										     salesuserimg=:upic 
								       WHERE SalesuserID=:uid');
			$stmt->bindParam(':uname',$SalesuserName);
			$stmt->bindParam(':ujob',$userjob);
			$stmt->bindParam(':upic',$salesuserimg);
			$stmt->bindParam(':uid',$id);
				
			if($stmt->execute()){
				?>
                <img src="data:image/gif;" />
                <?php
			}
			else{
				$Message_error = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	

Insert,Update,Delete,Upload an Picture or Image using PHP with MySQL

<html>
<head>
<title>Upload, Insert, Update, Delete an Picture or Image using PHP MySQL - Coding Cage</title>
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap.min.css">
<!-- linking - Optional - data theme -->
<link rel="stylesheet" href="Yourprojectfolder/css/bootstrap-theme.min.css">
<!-- link custom stylesheet -->
<link rel="stylesheet" href="Yourprojectfolder/style.css">
<!-- New Latest compiled js and minified JavaScript(js) -->
<img src="data:image/gif;" />

<img src="data:image/gif" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
 
<div class="navbar-header">
            <a class="navbar-brand" href="https://pakainfo.com/" title='Programming Blog'>Coding Cage</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/php">PHP</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/javascript">JavaScript</a>
            <a class="navbar-brand" href="https://pakainfo.com/search/label/jQuery">jQuery</a>
        </div>
    </div>

</div>
<div class="container">
<div class="page-header">
<h1 class="h2">Welcome - update profile. <a class="btn btn-default" href="index.php"> all members </a></h1>
    </div>
<div class="clearfix"></div>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
    <?php if(isset($Message_error)){ ?>

<div class="alert alert-danger">
          <span class="glyphicon glyphicon-info-sign"></span>   <?php echo $Message_error; ?>
        </div>

        <?php } ?>
<table class="table table-bordered table-responsive">
	
<tr>
<td><label class="control-label">SalesuserName.</label></td>
<td><input class="form-control" type="text" name="sl_user_name" value="<?php echo $SalesuserName; ?>" required /></td>

    </tr>

<tr>

<td><label class="control-label">Profession(Job).</label></td>

<td><input class="form-control" type="text" name="title_user_job" value="<?php echo $SalespersonUser; ?>" required /></td>

    </tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td>
<img src="user_Picture or Images/<?php echo $salesuserimg; ?>" height="150" width="150" />

        	<input class="input-group" type="file" name="user_Picture or Image" accept="Picture or Image/*" />
        </td>

    </tr>
<tr>

<td colspan="2"><button type="submit" name="save_bttn_update_data" class="btn btn-default">
        <span class="glyphicon glyphicon-save"></span> Data Update
        </button>
        
        <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-backward"></span> cancel Record </a>
        
        </td>

    </tr>
    </table>
</form>
<div class="alert alert-info">
    <strong>tutorial link !</strong> <a href="https://pakainfo.com/2016/02/upload-insert-update-delete-Picture or Image-using.html">Coding Cage</a>!
</div>
</div>
</body>
</html>