Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Friday, June 17, 2016

Multiple Insert Update Delete example using PHP MySQLi

This tutorial will guide you that how to perform multiple insert, update and delete operations using PHP & MySQLi, using this script you can update or delete multiple rows of MySQL database at a time on checkboxes selection with multiple insert, in my previous tutorial we have seen that how to Select / Deselect all checkboxes using jQuery, so using jQuery we can select all or multiple records from MySQL database and perform update or delete operations with the selected records, for better user interface i have used here again Bootstrap framework, we already have CRUD tutorials in this blog but i haven't cover this topic yet, so let's see the code.
Multiple Insert, Update, Delete (CRUD) using PHP & MySQLi
Read more »

Wednesday, June 15, 2016

Delete multiple records

Sometimes we may want to delete multiple records at once for more efficient work. We should provide checkbox for each records and users can select which records to delete.

In this example we should first create a database, a table and insert some data.
Type this sql script:
create database delsampel ;
use delsampel;
create table tabledelete (id int(5) primary key auto_increment, name varchar(50), position varchar(20), level int(2));
insert into tabledelete values(,Janet Jackson,Admin 2,8);
insert into tabledelete values(,Tommy Leo,Admin 1,5);
insert into tabledelete values(,John Connery,Admin 3,4);

then we should create 2 files
1. view.php
2. delete.php

below is the script for both files:
<?php
/*  view.php */
$host = "localhost";
$username = "root";
$password = "";
$database = "delsampel";
$connection = mysql_connect($host, $username, $password);
mysql_select_db($database, $connection) or die("MysQL Error");
$sql = mysql_query("select * from tabledelete");
?>
<html><body>
<form action="delete.php" method="POST">
<?php
echo "<table border=1>";
echo "<tr><th> </th><th>name</th><th>position</th><th>level</th></tr>";
while ($result = mysql_fetch_array($sql))
{
echo "<tr><td><input type=checkbox name=del[] id=del value=$result[id]></td><td>$result[name] </td><td> $result[position] </td><td> $result[level]</td></tr>";
}
?>
</table>
<input type="submit" name="justdel" value="Delete !!" id="justdel">
</form>
</body></html>

delete.php
<?php
/* delete.php */
$host = "localhost";
$username = "root";
$password = "";
$database = "delsampel";
$connection = mysql_connect($host, $username, $password);
mysql_select_db($database, $connection) or die("MysQL Error");
$id = $_POST[del];
$count = count($id); //counting how many rows (from checkbox) to delete
if($_POST[justdel])
{
for ($i=0; $i<$count; $i++)
{
$sql = mysql_query("delete from tabledelete where id=$id[$i]");
}
if ($sql)
{
print "Record successfully deleted<br>";
print "<a href=view.php>Click here to go back</a>";
}
}
?>

those example files can be downloaded here!! 2kb


Monday, June 13, 2016

Update Multiple Records Based On Radio Buttons Using FOR EACH

Today I will share a code snippet which will enable you to update multiple records based on radio buttons selection.

Consider this scenario:

Players are applying to play for a football team. There are more players than there are places, so some will get to play for the First team, some for the B team, some will be in the Reserves, and some will not get selected at all.

If you want to be able to pull up all players and assign their application result in the database in one update process.

Now you can do it using FOR EACH as shown in the snippet below:


foreach ($_POST[result] as $id => $result) {

$results[$result][] = $id;

}

$query = "UPDATE tablename SET result = CASE ";

foreach ($results as $res => $ids) {

$query .= sprintf("WHEN id IN (%s) THEN %s ", join(,, $ids), $res);

}

$query .= END;


Hope php script can guide you in the right direction.

Enjoy!

Saturday, June 11, 2016

Fetch Data from Multiple Tables with PHP MySQL

This tutorial is about, how to JOIN more than one tables in PHP using SQL join statement and without join statement, how to display results from MySQL database and show them in proper format, The act of joining two or more tables in MySQL refers to combine two or more tables into a single table, let's take a look.


Read more »