Edit multiple records using checkbox in php

First of all create database and table. Then create two php file i.e. index.php and edit.php.
Index.php Code

<?php
$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="student";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
<script language="javascript">
function checkThemAll(){
var formName = "form1";
var checkBoxName="checkbox[]";
var form = document.forms[formName];
var noOfCheckBoxes = form[checkBoxName].length;
var i = 0;
for(var x=0;x<noOfCheckBoxes;x++)
{
if(form[checkBoxName][x].checked==true){
i++;
}
}
if(i>1)
alert("You have selected only one rows");
else

var id=$_GET['check'];
return false;

}
</script>
<?php
session_start();
if(isset($_SESSION['update']))
{
$msg=$_SESSION['update'];
echo "<h2>"; echo $msg;echo "</h2>";
unset($_SESSION['update']);
}
?>
<table width="600" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="" onSubmit="return validate();">
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#ddd">
<tr>
<td>&nbsp;</td>
<td colspan="4"><strong>Edit Multiple Records using Checkbox in PHP</strong> </td>
</tr>
<tr><td></td></tr>
<tr>
<td></td>
<td style=" width:10%"><strong>Id</strong></td>
<td style=" width:30%"><strong>Name</strong></td>
<td style=" width:20%"><strong>Class</strong></td>
<td style=" width:40%"><strong>Email</strong></td>

</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['class']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td><input name="edit" type="submit" id="delete" value="Edit" Onclick="return checkThemAll();"></td>
</tr>

<?php

if(isset($_POST['edit']))
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$id=$_POST['checkbox'][$i];
$_SESSION['id']=$id;
header("Location: edit.php");
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Vineet Kumar Saini

 

 

 

 

 

If click on edit button without select any checkbox then get error like as follows

Vineet Kumar Saini

 

 

 

 

 

 

If click on edit button with all selected checkboxes then get error like as follows

Vineet Kumar Saini

 

 

 

 

 

 

Now select any checkbox then click on edit button.Then you will go to edit page.

Vineet Kumar Saini

 

 

 

 

 

 

Edit.php code

<?php
$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="student";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

session_start();
$id=$_SESSION['id'];
$sql="SELECT * FROM $tbl_name where id=$id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Multiple Edit in PHP</title>
</head>

<body>
<form method="post">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id" value="<?php echo $rows['id']; ?>" readonly /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $rows['name']; ?>"/></td>
</tr>
<tr>
<td>Class</td>
<td><input type="text" name="class" value="<?php echo $rows['class']; ?>"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $rows['email']; ?>"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="save" value="save" /></td>
</tr>
</table>
<?php
}
if(isset($_POST['save']))
{
$id_save = $_POST['id'];
$name_save = $_POST['name'];
$class_save = $_POST['class'];
$email_save = $_POST['email'];

mysql_query("UPDATE student SET id ='$id_save', name='$name_save',
class='$class_save',email ='$email_save' WHERE ID = '$id'")
or die(mysql_error());
$_SESSION['update']="Successfully Updated !";
header("Location: index.php");
}
mysql_close();
?>
</body>
</html>

Now you will get updated data.

Vineet Kumar Saini

Advertisements
Categories: Joomla, PHP & Joomla | Tags: , , , , , , , | 5 Comments

Post navigation

5 thoughts on “Edit multiple records using checkbox in php

  1. While using this code it gives error “Warning: Cannot modify header information – headers already sent by” How to solve this.Help me out. I used exit(); after the header but it didnot solved.

  2. Keshav – This might be happening due to the fact that you have not saved file in encoding of ANSI mode, it might be in UTF-8 or unicode etc. This is because the header works in php only when it should not have any input before it. that means in layman language that there should not be any code before that header and if you save your php file in UTF-8 encoding then it counts spaces and line breaks also.

    So the final solution is save your php files in ANSI encoding in notepad, then you wont get this error.

  3. roshan pawar

    sir i am roshan pawar above code is warning so thats why the code is not run means not update so that why when program is start that time and end program is use then our problem is solve

  4. akshay

    thanks bro it helped me a lot

  5. pawan attarkar

    thank you vineet sir….its really nice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: