This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Deleing Multiple Rows Using Checkboxes, PHP and MySQL
I wonder whether someone may be able to help me please.
I'm trying to put together a script which creates a form which gives the user the ability to delete a record via the selection of a checkbox and then pressing a 'submit' button.
From reading through many articles, I've put together the following script which is the section of code that builds the table, checkboxes and submit button.
<?php
$query = "SELECT l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
$result=mysql_query($query);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="del" id="del" action="deletelocation.php" method="post">
<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['locationid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['returnedaddress']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['totalfinds']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input type="submit" value="Delete" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
The code below, then deals with the deletion of the record.
<?php
$del_id = $_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $detectinglocations WHERE locationid='$del_id'";
$result = mysql_query($sql);
}
?>
The correct information is retrieved and shown in the form table, but the problem I'm having is that I'm unable to get the deletion of the record to work. I've run this through JavaScript Console, but unfortunately I don't receive an error message which may help me to solve the problem.
I just wondered whether someone could possibly take a look at this please and let me know where I'm going wrong.
Change your PHP code as below
$del_id = $_POST['checkbox'];
$detectinglocations = 'your database table name';
foreach($del_id as $value){
$sql = "DELETE FROM ".$detectinglocations." WHERE id='".$value."'";
$result = mysql_query($sql);
}
Related
Im trying to show the respective comments to the respective question ID but all the comments are shown in every post.
actually the value of qusetion ID is not going to the desired table where it suppose to be and that table
name: add_topic.php
<body>
<?php
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
// Get value of id that sent from hidden field
$id=$_POST['id'];
// Find highest answer number.
$q="SELECT MAX(a_id) AS Maxa_id FROM forum_ans WHERE que_id='$id'";
$result=mysqli_query($con,$q);
$row=mysqli_fetch_assoc($result);
// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($row) {
$Max_id = $row['Maxa_id']+1;
}
else {
$Max_id = 1;
}
// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_ans=$_POST['a_ans'];
$datetime=date("d/m/y H:i:s"); // create date and time
// Insert answer
$q2="INSERT INTO forum_ans(que_id, a_id, a_name, a_email, a_ans, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_ans', '$datetime')";
$result2=mysqli_query($con,$q2);
if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";
// If added new answer, add value +1 in reply column
$q3="UPDATE forum_que SET reply='$Max_id' WHERE id='$id'";
$result3=mysqli_query($con,$q3);
}
else {
echo "ERROR";
}
// Close connection
mysqli_close($con);
?>
</body>
block that named as que_id is showing the value of 0 (as i have set 0 as its default value) and that is the main reason of it but im not able to solve this problem. plz help...
name: view_topic.php
<body>
<?php
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
// get value of id that sent from address bar
$id=$_GET['id'];
$q="SELECT * FROM forum_que WHERE id='$id'";
$result=mysqli_query($con,$q);
$row=mysqli_fetch_assoc($result);
?>
<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><?php echo $row['topic']; ?></strong></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><?php echo $row['detail']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $row['name']; ?> <strong>Email : </strong><?php echo $row['email'];?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $row['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
// Switch to table "forum_answer"
$q2="SELECT * FROM forum_ans WHERE que_id='$id'";
$result2=mysqli_query($con,$q2);
while($row2=mysqli_fetch_assoc($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"><?php echo $row2['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"><?php echo $row2['a_name']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Email</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_email']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Answer</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_ans']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$q3="SELECT view FROM forum_que WHERE id='$id'";
$result3=mysqli_query($con,$q3);
$row3=mysqli_fetch_assoc($result3);
$view=$row3['view'];
// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$q4="INSERT INTO forum_que(view) VALUES('$view') WHERE id='$id'";
$result4=mysqli_query($con,$q4);
}
// count more value
$addview=$view+1;
$q5="update forum_que set view='$addview' WHERE id='$id'";
$result5=mysqli_query($con,$q5);
mysqli_close($con);
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_ans.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"></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45"></td>
</tr>
<tr>
<td valign="top"><strong>Answer</strong></td>
<td valign="top">:</td>
<td><textarea name="a_ans" cols="45" rows="3" id="a_ans"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
name: add_topic.php
<body>
<?php
// get data that sent from form
$topic=$_REQUEST['topic'];
$detail=$_REQUEST['detail'];
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
$q="INSERT INTO`forum`.`forum_que`(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
if(mysqli_query($con,$q))
{
echo '<script>alert("Data Inserted Now redirecting to login page");</script>';
header('location:main_forum.php');
}
else
{
echo "error inserting record" . mysqli_error($con,$query);
echo '<script>alert("Error while inserting data");</script>';
}
mysqli_close($con);
?>
</body>
You have two add_topic.php should one be add_ans.php
I don't see any exact problems with this code are you sure that the $id is being used correctly in the hidden field of your form?
Possibly try doing a print_r($_POST); on the add_ans.php page to see what data is actually being passed by the form
I know there are questions similar, but I am unable to determine my issue with my PHP code. I am trying to remove a student from an intermediary table between Student table and Class table, called Student_has_Class. The SQL code works, just not the page. It lists the students in the table successfully, but when I check the box by a student to remove them and click submit, it redirects to same page (basically a refresh) like it is supposed to (even though it doesn't reload the table for some reason), and the student is still in the Student_has_Class table. Please help, as posting online is a last resort for me.
Please take a look a the code:
Note: I removed some login and web address info for privacy. Indicated with { }.
<?php
// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];
mysql_connect("{removed site for privacy}", "{username removed}", "{password removed}") or die(mysql_error
());
mysql_select_db("test") or die(mysql_error());
if (isset($_POST['delete']))
{
if (isset($_POST['checkbox']))
{
$checkbox = $_POST['checkbox'];
if (is_array($checkbox)) {
foreach ($checkbox as $key => $x)
{
$mysql->query("DELETE FROM Student_has_Class WHERE User_idUser='$x'");
}
}
}
}
?>
<?php
$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";
$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>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First 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['idUser'];?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>
<?php
// Check if delete button active, start this
if ($_POST['delete'])
{
for ($i = 0; $i < $count; $i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE FROM Student_has_Class WHERE User_idUser=".$del_id."";
$result = mysql_query($sql);
}
// if successful redirect to same page
if ($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL={address}/{page}.php\">";
}
}
mysql_close();
?>
Update:
I used Internet Explorer debugger to get some info that Chrome debugger doesn't provide. Here is the page, notice the values in the checkboxes. I'm not sure if that is correct.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>*HIDDEN*</title>
<style type="text/css">
body {
background-color: #D9D7D7;
}
h1 {
color: #FBF8F8;
}
</style>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body bgcolor="#D7D3D3">
<a href="main_login.php" >
<img src="banner2.jpg" width="1340" height="90">
</a></img>
<div id="shHeader">
<center>
<div class="div">
Welcome to *HIDDEN*!</div></center>
<table width ="1345" height="30" align="top" border=1">
<tr align= top>
<tr>
<td width="40"> Category </td>
<td width="40"> Homework Graph </td>
<td width="40"> Test Graph </td>
<td width="40"> Overview Graph </td>
<td width="40"> Progress Bar </td>
<td width="40"> Timeline </td>
</tr>
<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>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[0]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">5</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[1]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">3</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[2]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">12</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[3]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">4</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[4]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">6</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<input type='hidden' name='hiddencounter' value='5'>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Update:
The POST parameters are as follows when I try to delete the third person in the table by checking the checkbox to the left of their name, and clicking the Delete button (previously called Submit button). The person to delete has a User ID of 12.
Array ( [checkbox] => Array ( [2] => ) [hiddencounter] => 5 [delete] => Delete )
You can try this. But I will change your code from MySQL to MySQLi.
<?php
// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabaseName"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if (isset($_POST['delete'])){ /* IF DELETE IS SET/CLICKED */
$counter=mysqli_real_escape_string($connect,$_POST['hiddencounter']);
$checkbox=$_POST['checkbox'];
for($x=0;$x<=$counter;$x++){
if(!empty($checkbox[$x])){ /* IF SELECTED ITEM */
$checked=mysqli_real_escape_string($connect,$checkbox[$x]);
mysqli_query($connect,"DELETE FROM Student_has_Class WHERE User_idUser='$checked'");
} /* END OF IF NOT EMPTY CHECKBOX SELECTED */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
?>
<?php
$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";
$result = mysqli_query($connect,$sql);
$count = mysqli_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>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>
<?php
$counter=0; /* FOR COUNTING PURPOSES */
while ($rows = mysqli_fetch_array($result))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<?php
$iduser=$rows['idUser'];
echo "<input name='checkbox[$counter]' type='checkbox' id='checkbox[]' value='$iduser'></td>";
/* I HAVE ASSIGNED THE COUNTER INSIDE THE CHECKBOX ARRAY ABOVE. AND DO YOU REALLY NEED [] IN YOUR ID? */ ?>
<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>
<?php
$counter=$counter+1; /* INCREMENT COUNTER EVERY LOOP */
} /* END OF WHILE LOOP */
echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* SUBMIT A HIDDEN INPUT CONTAINING THE OVERALL COUNTER */
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>
</table>
</form>
I have a page that displays all of the contents of my table. Alongisde each row of the table I also have a column containing a checkbox. I have implemented this for deleting rows. If in case i want to edit and save changes in the table how am i supposed to deal with?Below is the code which i have implemented for delete.
<?php
// Connect to server and select databse.
mysql_connect("localhost", "root", "")or die("cannot connect");
mysql_select_db("project")or die("cannot select DB");
$sql="SELECT * FROM menu";
$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>Vendor_id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Item_id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Item_name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Price</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['item_id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['vendor_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['item_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['item_name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['Price']; ?></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(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM menu WHERE item_id='$del_id'";
$result = mysql_query($sql);}
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_menu.php\">";}}
mysql_close();
?>
</table></form></td></tr></table>
first of all use PDO instead of mysql...
and for your answer ..
there are many ways to do this... but the simplest that i can think of is.. creating an edit button/(link) in each table rows. this sends the id of the data to be edited to a php page where u can do the editand saving it to the database table part...
for an example...
i will add this to your table..
HTML
<td bgcolor="#FFFFFF"><a href="saveEdit.php?id=".<?php echo $rows['vendor_id']; ?>EDIT</a></td>
this will take u to the saveEdit.php page with an id...
u can get the id value in saveEdit.php by
$vendor_id=$_GET['id'];
u have an id now...create a form...get related data of that vendor from the table and show it in the form...let user edit it...and save it to the database table..like u did, to delete the vendor..
I want to delete mySQL rows with checkboxes. This is a code that should work according to someone on the internet, but some reason it doesn't for me. When I click delete it only refreshes but the row doesn't disappear. Has this something to do with my ID in the table?
<body>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="*****"; // Mysql password
$db_name="test"; // Database name
$tbl_name="deviation"; // 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">Åtgärda</td>
<td align="center" bgcolor="#FFFFFF"><strong>Chassinummer</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Problem detail</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Fault code</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Position</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Help object</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation step</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['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['chassi']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['problem_detail']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fault_code']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fault_code']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['position']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['help_object']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['operation_step']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
**$checkbox = $_POST['checkbox'];**
**$delete = $_POST['delete'];**
// 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=deletetable.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
There are certain things I want to direct your attention towards:
1) There are a number of lines in PHP enclosed in nonsensical double-asterisks. I'm shocked the PHP actually ran through those.
2) You have a $result defined within the global scope at the top of your script (where it was given a resource pointer from mysql_query()). This means that at the bottom, where you check for if($result), that check will always come to true (unless there were syntactic errors). This also means that the page will always refresh when you click submit regardless of whether the deletion actually happened.
As I've mentioned in the comments, that code is (frankly speaking) a piece of crap. It's vulnerable to SQL injection, the code doesn't actually do what it's supposed to do, it's using deprecated attributes and functions... The failure of the code to do what you want isn't because of the IDs in your table, it's because of the broken, non-functioning code you copied off the Internet.
Don't iterate over whole table
Replace this
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
**$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";**
$result = mysql_query($sql);
}
with
if(isset($_POST['checkbox']) && is_array($_POST['checkbox')){
foreach($_POST['checkbox'] as $id){
$id = (int)$id;
$sql = "DELETE FROM $tbl_name WHERE id='$id'";
$result = mysql_query($sql);
}
}
I am trying to implement a UI which the user would be able to select entries by selecting checkboxes to delete them from the database. However, while everything looks fine and able to display, there is an error saying Notice: Undefined variable: delete in /opt/uiForm.php on line 154 whereas $delete is not readable. I am following the example given here. Am I missing anything?
<body>
<form name="frm" method="get" action="doSubmit();">
<?php
// Connect to server and select database.
mysql_connect("127.0.0.1:3306", "root", "")or die("cannot connect");
mysql_select_db("PushApplication")or die("cannot select DB");
$sql="SELECT * FROM Device";
$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" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceType</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>OS_Version</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Status</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['DeviceID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['DeviceType']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Description']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['OS_Version']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Status']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="7" 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 Device 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>
</form>
</body>
</html>
You really should be using
if (!empty($_POST['delete'])) {
instead of
if ($delete) {
to avoid the notice you are getting. Also your code is prone to the so called SQL injection vulnerability which you read upon if you are considering to put it somewhere on the web.
Also you should be using method="post" instead of GET for this type of operations.
I suspect the other example was not displaying all errors.
When checking for the existence of a variable, you need to use if(!empty($delete)), because if returns an error when the variable it's checking does not exist. empty() does not.
Your code supposes that server's PHP has option register_globals = On; Which is not true by default for the late versions of PHP setup. Read this What are register_globals in PHP?