Jump to content

[SOLVED] Delete record


Marcos01

Recommended Posts

Hi all,

 

I want to select a record from a list. I use a checkbox for that.

 

I am having trouble deleting the record. What am I doing wrong?

 

Here is the code:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<table cellpadding="5" cellspacing="5"><tr>';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td><label><input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/></label></td>';
echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>";

}

echo '</table><label>';
echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />';
echo '</label></form>';



if ($_POST['doaction']=='delete')
{ 
	$sql = "DELETE FROM trainingsdata WHERE id=".$row['id']."" ;
echo 'data deleted';
}

Link to comment
Share on other sites

Try this:

 

if ($_POST['doaction']=='delete')
   {
    $sql = "DELETE FROM trainingsdata WHERE id = ' ".$_POST['checkbox']." ' " ;
   echo 'data deleted';
   }

 

You need to put single quotations around variable so id could properly parsed.

 

In first query it wasn't parsed properly especially if it's a number.

 

I hope you can see the difference (don't just copy/paste the code, you need to take out the spaces!!!)

Link to comment
Share on other sites

Two things I would like to tell

 

First:

As you have more than many records instead of line

<input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/>

use

<input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/>

 

Second:

 

On posting the checkbox values will be available as an array. Check whether which all checkbox you have checked in by looping through the array

 

you can access the check box value using index like

 

$_POST[checkbox][0]

 

let me know whether this works for you

 

 

Link to comment
Share on other sites

Thanks budimir and ranjuvs for your help.

 

Sorry to say but still noting happens after I check a box and hit delete.

 

Here the code so far:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<table cellpadding="5" cellspacing="5"><tr>';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td><label><input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/></label></td>';
echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>";
}
echo '</table><label>';
echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />';
echo '</label></form>';
if ($_POST['doaction']=='delete')
{ 
	$sql = "DELETE FROM trainingsdata WHERE id='".$_POST['checkbox']."'" ;
echo 'data deleted';
}

Link to comment
Share on other sites

In my last post I mentioned that the value of $_POST['checkbox'] this will be array. You need to use index to get the value.

 

Try to loop through the array and find the value. The array variable which have 1 has its value is the one you checked.

 

$_POST['checkbox'][index]

where index - 0,1,2....

Link to comment
Share on other sites

$_POST['checkbox'] becomes an array so you need to loop through it like this

if ($_POST['doaction'] == 'delete') {
foreach ($_POST['checkbox'] as $id) {
	$sql = "DELETE FROM trainingsdata WHERE id='{$id}'";
	mysql_query($sql) or die(mysql_error());
	echo "{$id} data deleted";
}
}

 

Scott.

Link to comment
Share on other sites

Thanks ratcateme, I tried that but nothing happens after I select and hit delete.

 

Code so far:

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<table cellpadding="5" cellspacing="5"><tr>';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td><label><input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/></label></td>';
echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>";
}
echo '</table><label>';
echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />';
echo '</label></form>';

if ($_POST['doaction'] == 'delete') {
foreach ($_POST['checkbox'] as $id) {
	$sql = "DELETE FROM trainingsdata WHERE id='{$id}'";
	mysql_query($sql) or die(mysql_error());
	echo "{$id} data deleted";
}
}

Link to comment
Share on other sites

ratcateme, ranjuvs and budimir,

 

Thanks for your help!

 

Here the final code:

 

$result = mysql_query("SELECT id, date, course, place FROM data ") or die(mysql_error());
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<table cellpadding="5" cellspacing="5"><tr>';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td><label><input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/></label></td>';
echo "<td>".$row['date']."</td><td>".$row['course']."</td><td> ".$row['place']."</td><tr>";
}
echo '</table><label>';
echo '<input type="submit" name="doaction" id={"delete"'.$row['id'].'} value="delete" /></label></form>';

if ($_POST['doaction'] == 'delete') {
foreach ($_POST['checkbox'] as $id) {
	$sql = "DELETE FROM data WHERE id='{$id}'";
	mysql_query($sql) or die(mysql_error());
	echo "date gewist";
	echo '<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'">';
}
}

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.