Deleting A number of Information with mysqli in PHP Utilizing Type Enter

[ad_1]

Right this moment I’ll focus on an instance of Deleting A number of Information with mysqli in PHP Utilizing Type Enter.

Suppose we now have a database desk and we need to enable the consumer to delete some or the entire information. Since consumer will entry the applying via an online interface, the delete possibility must be out there on the net web page. So, first we make all of the information exhibited to the consumer. Additional, a checkbox can also be there with every document and consumer want to pick out the information he need to delete. The next screenshot exhibits the shape that shows the information.

The Web Form for Deleting Multiple Records with mysqli in PHP
The Internet Type for Deleting A number of Information with mysqli in PHP

When consumer clicks on the ‘Delete Information’ button, the SQL Delete command runs for all the chosen information. The next code exhibits how you can show the end result set of a. SQL Choose question in an HTML type and how you can run the delete command for a number of information.

The Program for Deleting A number of Information with mysqli in PHP Utilizing Type Enter

deleterecords.php

<html>
 <head>
  <title>Deleting Information</title>
 </head>
 <physique>
<?php
$host="localhost";
 $consumer="root";
 $cross="";
 $db='institute';
 
 $con=new mysqli($host, $consumer, $cross, $db);
 
 if($con->error)
 {
    die('Error connecting to the database '.$con->error);
 }
 else
 {
   echo 'Related!<br>';
 }

$q='choose * from emp';
 $retrieved_data=$con->question($q);
?>
<type identify="f1" methodology="put up" motion="<?php echo $_SERVER['PHP_SELF']; ?>">
<desk border="0" cellpadding="5" cellspacing="5">
 <tr>
    <th>Worker ID</th>
    <th>Worker Title</th>
    <th>Wage</th>
    <th>Designation</th>
    <th>Division</th>
    <th>Take away</th>
 </tr>
    
<?php
 whereas($r=$retrieved_data->fetch_assoc())
 {
        $t=$r['emp_id'];
        echo '<tr>';
	echo '<td>', $r['emp_id'], '</td>';
	echo '<td>', $r['emp_name'], '</td>';
	echo '<td>', $r['salary'], '</td>';
	echo '<td>', $r['designation'], '</td>';
	echo '<td>', $r['dept_no'], '</td>';
        echo '<td><enter kind="checkbox" identify="ch[]" worth="'.$t.'"/></td>';
        echo '<tr/>';
 }
 
?>
</desk>
<enter kind="submit" identify="submit" worth="Delete Information"/>
</type>
<?php
if(isset($_POST['submit']))
{
  $question="delete from emp the place emp_id=?";
  $assertion=$con->put together($question);
  if(!empty($_POST['ch'])){
   foreach($_POST['ch'] as $p)
   {   echo $p;
       $statement->bind_param("i", $p);
       $statement->execute();	
   }	
  }
  header("Location: show.php");
}
$con->shut();
?>
</physique>
</html>

At first, we make a reference to the database. After that, the question is executed utilizing the question() methodology and the variable retrieved_data will get the end result set. The shape shows the retrieved information in an HTML desk. Additionally, be aware using fetch_assoc() perform that returns a document from the end result set at a time. Subsequently, the the whereas loop creates a row of HTML desk and shows all of the fields of that row in every iteration. Every row additionally shows a checkbox. Moreover, the alternatives made by the consumer will be obtained as an array. the next picture exhibits the consumer choice.

Output

Selecting Records for Deletion
Choosing Information for Deletion

As soon as, the consumer makes the choice, he presses the Delete Report button. In consequence, the particular PHP script executes and it runs the delete question as a ready assertion for all the chosen information in a loop. When the queries end executing, the header() perform opens the show.php file that shows the information after deletion.

Records After Deletion
Information After Deletion

show.php

<html>
 <head>
  <title>Worker Information</title>
 </head>
 <physique>
<?php
$host="localhost";
 $consumer="root";
 $cross="";
 $db='institute';
 
 $con=new mysqli($host, $consumer, $cross, $db);
 
 if($con->error)
 {
    die('Error connecting to the database '.$con->error);
 }
 else
 {
   echo 'Related!<br>';
 }

$q='choose * from emp';
 $retrieved_data=$con->question($q);
?>
<desk border="0" cellpadding="5" cellspacing="5">
 <tr>
    <th>Worker ID</th>
    <th>Worker Title</th>
    <th>Wage</th>
    <th>Designation</th>
    <th>Division</th>
 </tr>
    
<?php
 whereas($r=$retrieved_data->fetch_assoc())
 {
        echo '<tr>';
	echo '<td>', $r['emp_id'], '</td>';
	echo '<td>', $r['emp_name'], '</td>';
	echo '<td>', $r['salary'], '</td>';
	echo '<td>', $r['designation'], '</td>';
	echo '<td>', $r['dept_no'], '</td>';
        echo '<tr/>';
 }
 
?>
</desk>
</physique>
</html>


Additional Studying

Examples of Array Capabilities in PHP

Primary Applications in PHP

Registration Type Utilizing PDO in PHP

Inserting Data from A number of CheckBox Choice in a Database Desk in PHP

PHP Tasks for Undergraduate College students

Architectural Constraints of REST API

REST API Ideas

Making a Labeled Advertisements Utility in PHP

programmingempire

princites.com

[ad_2]

Source_link

Leave a Comment