Jump to content

Delete table entry help.


gangsterwanster1

Recommended Posts

First select the primary key and a title field from the table and loop out the rows within a form. You will create an array of checkboxes with the primary key as the value i.e.

print "<form method='post'>\n";
print "<input type='hidden' name='action' value='delete' />\n";
// select data
$result = mysql_query("SELECT id, title FROM table ORDER BY title ASC");
while($row = mysql_fetch_object($result)) {
print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n";
}
print "<input type='submit' name='submit' value='delete selected' />\n";
print "</form>";

We then will add the code that does the deletion. This should be right at the top of the page, prior to anything being printed on the screen:

// delete button selected
if($_POST['action'] && $_POST['action'] == 'delete') {
if(count($_POST['values'])) {
	// delete records
	mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")");
	// reload page
	header("Location:abc.php");
	exit();
}
}

The whole script will look as follows

<?php
// delete button selected
if($_POST['action'] && $_POST['action'] == 'delete') {
if(count($_POST['values'])) {
	// delete records
	mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")");
	// reload page
	header("Location:abc.php");
	exit();
}
}

print "<form method='post'>\n";
print "<input type='hidden' name='action' value='delete' />\n";
// select data
$result = mysql_query("SELECT id, title FROM table ORDER BY title ASC");
while($row = mysql_fetch_object($result)) {
print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n";
}
print "<input type='submit' name='submit' value='delete selected' />\n";
print "</form>";
?>

Link to comment
Share on other sites

First select the primary key and a title field from the table and loop out the rows within a form. You will create an array of checkboxes with the primary key as the value i.e.

print "<form method='post'>\n";
print "<input type='hidden' name='action' value='delete' />\n";
// select data
$result = mysql_query("SELECT id, title FROM table ORDER BY title ASC");
while($row = mysql_fetch_object($result)) {
print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n";
}
print "<input type='submit' name='submit' value='delete selected' />\n";
print "</form>";

We then will add the code that does the deletion. This should be right at the top of the page, prior to anything being printed on the screen:

// delete button selected
if($_POST['action'] && $_POST['action'] == 'delete') {
if(count($_POST['values'])) {
	// delete records
	mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")");
	// reload page
	header("Location:abc.php");
	exit();
}
}

The whole script will look as follows

<?php
// delete button selected
if($_POST['action'] && $_POST['action'] == 'delete') {
if(count($_POST['values'])) {
	// delete records
	mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")");
	// reload page
	header("Location:abc.php");
	exit();
}
}

print "<form method='post'>\n";
print "<input type='hidden' name='action' value='delete' />\n";
// select data
$result = mysql_query("SELECT id, title FROM table ORDER BY title ASC");
while($row = mysql_fetch_object($result)) {
print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n";
}
print "<input type='submit' name='submit' value='delete selected' />\n";
print "</form>";
?>

 

I get this error;

 

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\test.php on line 21

 

'while($row = mysql_fetch_object($result)) {'

 

Any idea why i get this? All i added was mysqlconnect // mysqlselect database

Link to comment
Share on other sites

please provide your connection code

 

I got it to work perfectly for the test but now i am having troubles implementing it in my main code.

 

<?php
mysql_connect("localhost", "root", "") 
mysql_select_db("maindb") 

// delete button selected
if($_POST['action'] && $_POST['action'] == 'delete') {
   if(count($_POST['input'])) {
      // delete records
      mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['input']).")");
      // reload page
      header("Location:test.php");
      exit();
   }
}

print "<form method='post'>\n";
print "<input type='hidden' name='action' value='delete' />\n";
// select data
$result = mysql_query("SELECT cid, title FROM logs ORDER BY title ASC");
while($row = mysql_fetch_object($result)) {
   print "<input type='checkbox' name='input[]' value='".$row->id."' /> ".$row->title."<br />\n";
}
print "<input type='submit' name='submit' value='delete selected' />\n";
print "</form>";
?>

Can you provide an example that uses the above method but does the following?

1.) Makes four rows (username, date, comment, delete check mark)

So like this,

Bill - 2009 - Loves - [] check mark

John -2009 - Hates - [] checkmark  (the four rows are four columns from the database)

 

2.) Once the information has been read from the database and the rows are shown with the four separate areas then at the bottom of the page can there be 1 delete button, which if i select any of the check marks on the page it automatically deletes it?

 

Sorry for asking for so much i have been working on this for a while and i cant figure it out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.