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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-643646
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-643648
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-643650
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-643657
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-644068
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-644583
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
https://forums.phpfreaks.com/topic/124623-solved-delete-record/#findComment-645596
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.