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..
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!
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);
}
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?