Can any one tell what is happening with my code? I got this code from web but its not working; when I press delete its not deleting the database. Can you help me?
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="comment";
$tbl_name="members";
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td></tr>
<tr><td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
< td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td></tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
<tr><td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
</td><td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['firstname']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['passwd']; ?>
</td></tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td></tr>
</table></form></td></tr></table>
<?php
$checkbox = $_POST['checkbox'];
$delete = $_POST['delete'];
if(isset($delete)){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";}}
mysql_close();
?>
Please correct the form action to work the code
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="comment";
$tbl_name="members";
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="<? $_SERVER["PHP_SELF"]; ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF">
<strong>Delete multiple rows in mysql</strong>
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Id</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Name</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Lastname</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Email</strong>
</td>
</tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[<? echo $rows['id']; ?>]" type="checkbox" value="<? echo $rows['id']; ?>">
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['id']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['firstname']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['lastname']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['passwd']; ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$delete = $_POST['delete'];
if(isset($delete)){
$t = $_POST;
foreach($t as $param){
$t1 = $param;
if(is_array($t1)){
foreach($t1 as $param2){
echo $param2;
echo "<br>";
//Here execute delete query
}
}
}
}
?>
Related
I'm learning php, now I'm trying to get use with databases and etc stuff. My code (that is below) seems that is not working, and I can't solve it out why. It looks good but still after pushing "delete" nothing happens. Maybe you will be able to help me. Thanks in advance.
<?php
$host="localhost";
$username="root";
$password="";
$db_name="zurnalas";
$tbl_name="zurnalas";
// Connect to server and select databse.
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);
?>
<form name="form1" method="post" action="">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Žurnalų trinimas iš duomenų bazės</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kiekis</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Leidykla</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Metai</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kaina</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['pavadinimas']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kiekis']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['leidykla']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['metai']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kaina']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($_POST['delete']){
$checkbox[] = $_POST['checkbox[]'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=trinti.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Your form's action field is empty. You have to get fields from form with php "$checkbox = $_POST['checkbox']". After you can do like you do, or you can write other mysql query " DELETE FROM YOUR_TABLE WHERE id in (1,2,3,4) ".
I'm Trying To Delete Multiple Rows By Using Check Boxes. But The Values Are Not Deleting.
I Don't Understand Where I'm Going Wrong.
Please Help Me.
Connect.php
<?php
$host="localhost"; // Host name
$username="DB_USERNAME"; // Mysql username
$password="DB_PASSWORD"; // Mysql password
$db_name="DB_NAME"; // Database name
$tbl_name="TABLE_NAME"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
exe.php
<?php
include "connect.php";
// Check if delete button active, start this
if($_POST['delete'])
{
$id = $_POST['data'];
$count = count($id);
for($i=0;$i<$count;$i++){
//echo "<br> value = ".$id[$i]."Jumlah = ".$count ;
$sql = "DELETE FROM $tbl_name WHERE id='$id[$i]'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}
}
mysql_close();
?>
index.php
<?php
include "connect.php";
$result=mysql_query("SELECT * FROM $tbl_name ORDER BY ts DESC");
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="exe.php">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Order Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="data[]" type="checkbox" id="data" value="<?php echo $rows['oid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['oid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['user']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['data']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['type']; ?></td>
</tr>
<?php
}unset($rows);
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
Someone please help me with this and tell me where am i wrong. I'm still learning PHP.
Instead of using a loop an running several SQL queries, you can just use the built in SQL WHERE IN clause:
$ids = join(',',$id); //same as using implode()
$sql = "DELETE FROM $tbl_name WHERE id IN '$ids'";
It will be more efficient!
Hope this helps!
So, I'm adding a forum section to my site. So far, everyone thing is good. You can create a topic, view a list of topics, but when you go to view a topic (which is view_topic.php?id=#) this keeps appearing:
Parse error: syntax error, unexpected $end in /home/papervip/public_html/forums/questions/view_topic.php on line 158
So, here's the full script of view_topic.php (without the database credentials included, so I can ensure my security :D ):
<?php
session_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="forum_questions"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../style/base.css">
<title>Paperviper - Questions - View Topic</title>
<meta name="keywords" content="games, free, indie, paperviper, pc, video games">
<meta name="description" content="Paperviper is a game developer group consisten of a team of indie-developers and freelancers. Working to bring you the best cheap and free games for addicting fun on your PC, Mac, and Linux!">
<style type="text/css">
</head>
<body>
<?php if(empty($_SESSION['user'])){
include("../../header.html");
}else{
include("../../user/user_header.html");
?>
<div id="page">
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong><? echo $rows['topic']; ?></strong></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['name']; ?> <strong>Email : </strong><? echo $rows['email'];?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
$tbl_name2="forum_questions_answer";
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_id']; ?></td>
</tr>
<tr>
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>
<td width="5%" bgcolor="#F8F7F1">:</td>
<td width="77%" bgcolor="#F8F7F1"><? echo $rows['a_name']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Email</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_email']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Reply</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_answer']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($sql3);
$rows=mysql_fetch_array($result3);
$view=$rows['view'];
/
if(empty($view)){
$view=1;
$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}
$addview=$view+1;
$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);
mysql_close();
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_answer.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="18%"><strong>Name</strong></td>
<td width="3%">:</td>
<td width="79%"><input name="a_name" type="text" id="a_name" size="45" value="<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>" readonly></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45" value="<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>" readonly></td>
</tr>
<tr>
<td valign="top"><strong>Reply</strong></td>
<td valign="top">:</td>
<td><textarea name="a_answer" cols="38" rows="10" id="a_answer"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit" class="button"> <input type="reset" name="Submit2" value="Reset" class="button"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php include("../../footer.html");?>
</div>
</body>
</html>
I've re-read the entire file about 10 times now, but it still keeps stating the final closing html tag as a unexpected $end. Am I overlooking something? Thanks in advance.
I believe you have an unmatched { here:
<body>
<?php if(empty($_SESSION['user'])){
include("../../header.html");
}else{
include("../../user/user_header.html");
} // It looks like this is missing
?>
I am trying very hard for the last 2 hours but am not able to understand. Why am I getting undefined variable in DELETE. Please please look in to my code and help me out.
My main intention is to delete multiple rows at once using checkboxes.
When I am trying to run the below codes it gives me following errors:
Undefined variable: delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1.php on line 61
But I already have used it in following way:
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
Thanks in advance.
my php codes :
<?php
$host="localhost"; // Host name
$username="a"; // Mysql username
$password="a"; // Mysql password
$db_name="b"; // Database name
$tbl_name="emp_info"; // Table name
// Connect to server and select databse.
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Address</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Source</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>salary</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Zip</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Mobile</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['eid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['ename']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['address']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['source']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['salary']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['zip']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['mobile']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE eid='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to search1.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Register globals is off (and this is good) so the line
if($delete){
should be
if (isset($_POST['delete'])) { ...
Moreover you need to restructure your code as it's absolutely unnecessery to make a select query to database when you have a post request to delete records.
Here is a complete code you need. Just replace yours with this one.
<?php
error_reporting(E_ALL);
$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Delete records
if (isset($_POST['delete']))
{
// Concatenate ids in a comma-separated string
$ids = implode(',', $_POST['checkbox']);
if (!empty($ids))
{
$sql = "DELETE FROM $tbl_name WHERE eid IN ($ids)";
$result = mysql_query($sql) or die(mysql_error());
}
// Redirect back
header('Location: search1.php');
die();
}
// Select records
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)) { ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]"
value="<?php echo $rows['eid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
Before looping results from MySQL result variable, you should ALWAYS have to make sure that it contain any values:
if($total > 0)
{
while ($rows = mysql_fetch_array($result))
{
//your code goes here
}
}
Another issue you have here, is same as in your previous question, variable $delete is not defined, based on your code, it is not clear where it is coming from.
So, if $delete is actually a $_POST variable, your if statement should look like this:
if(isset($_POST['delete'])){}
Another issue, $checkbox it is not defined to. I would recommend you to read more about filter_input method as it can be very handy on sanitizing user submitted data.
Also you are using incorrect html tags instead of:
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
use correct TD tag which aligns text in center and makes it bold, also bgcolor should be defined in CSS file.
<th bgcolor="#FFFFFF">Id</th>
Your final code should look like this:
<?php
$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Make sure that delete action was called using
//$delete = isset($_POST['delete']);
//OR
//$delete = array_key_exists('delete', $_POST);
//OR
$delete = filter_input(INPUT_POST, 'delete');
$selected = filter_input(INPUT_POST, 'checkbox', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);
if ($delete && !empty($selected))
{
foreach ($selected as $id)
{
$sql = "DELETE FROM $tbl_name WHERE eid='$id'";
$result = mysql_query($sql);
}
// if successful redirect to search1.php
echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
}
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<th bgcolor="#FFFFFF"> </th>
<th colspan="9" bgcolor="#FFFFFF">Delete multiple rows in mysql</th>
</tr>
<tr>
<th bgcolor="#FFFFFF">#</th>
<th bgcolor="#FFFFFF">Id</th>
<th bgcolor="#FFFFFF">Name</th>
<th bgcolor="#FFFFFF">Password</th>
<th bgcolor="#FFFFFF">Address</th>
<th bgcolor="#FFFFFF">Source</th>
<th bgcolor="#FFFFFF">salary</th>
<th bgcolor="#FFFFFF">Zip</th>
<th bgcolor="#FFFFFF">Mobile</th>
<th bgcolor="#FFFFFF">Email</th>
</tr>
<?php
if ($count)
{
while ($rows = mysql_fetch_array($result))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox" value="<?php echo $rows['eid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['address']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['source']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['salary']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['zip']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['mobile']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
}
mysql_close();
?>
<tr>
<td colspan="10" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
Please don't just copy paste it, read it and try to understand it.
Here is my code -
<?php
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "test"; // Database name
$tbl_name = "test_mysql"; // Table name
// Connect to server and select databse.
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while ($rows = mysql_fetch_array($result)) {
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox"
id="checkbox[]" value="<? echo $rows['id']; ?>">
</td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete"
value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if ($delete) {
for ($i = 0; $i < $count; $i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if ($result) {
echo "<meta http-equiv="refresh" content="0;URL = delete_multiple . php">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
What I want is that data rows of the table, whose checkboxes are checked,
to be Deleted from the Database on click of Delete button.
I have tried these but it doesn't work well.
Please suggest me regarding the above code how to delete the selected rows
from DB, onclick of Delete button.
Theoretically it should work, but I have not tested
<?php
$host = 'localhost'; // Host name
$username = ''; // Mysql username
$password = ''; // Mysql password
$db_name = 'test'; // Database name
$tbl_name = 'test_mysql'; // Table name
// Connect to server and select databse.
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)): ?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['name']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['lastname']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['email']); ?></td>
</tr>
<?php endwhile; ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if ( ! empty($_POST['delete'])) {
foreach ($_POST['need_delete'] as $id => $value) {
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;
mysql_query($sql);
}
header('Location: delete_multiple.php'); exit();
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Tested version:
Change lines 45-55 to
if($_POST['delete']){
$i = 0;
while(list($key, $val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id='$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
// if successful redirect to delete_multiple.php
if($i > 0){
echo '<meta http-equiv="refresh" content="0;URL=delete_multiple.php">';
}
}
The file should start with "<?php" and it is considered bad style to mix "<?" and "<?php" in the same file (lines 31-35 and line 43).
EDIT: forgot the $_POST[''] from $delete on line 45. If you have the register_globals ini-directive on, it does not matter (It's still dangerous and bad style though).