/    Sign up×
Community /Pin to ProfileBookmark

Deleting data in table

Hi,
I have a simple script that connects to my MySQL database, and shows me the data that I have requested in a table layout.

I have searched a few guides and I am struggling with the next bit though, I want to display a checkbox at the end of each row and select and then a button to delete. Any ideas on how I can do this?

here is my code so far that calls the information

[code=php]<?
mysql_connect(“”,””,””) or die(mysql_error());
mysql_select_db(“”) or die(mysql_error());

$query = “SELECT * FROM “;

echo “<table style=’width:100%’><tr>”;
echo “<td>Title</td>”;
echo “<td>Name</td>”;
echo “<td>Company</td>”;
echo “<td>Address1</td>”;
echo “<td>Address2</td>”;
echo “<td>Town</td>”;
echo “<td>County</td>”;
echo “<td>Postcode</td>”;
echo “</tr>”;

$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

echo “<td>”.$row[‘title’].”</td>”;
echo “<td>”.$row[‘name’].”</td>”;
echo “<td>”.$row[‘company’].”</td>”;
echo “<td>”.$row[‘address1’].”</td>”;
echo “<td>”.$row[‘address2’].”</td>”;
echo “<td>”.$row[‘town’].”</td>”;
echo “<td>”.$row[‘county’].”</td>”;
echo “<td>”.$row[‘postcode’].”</td>”;
echo “</tr>”;
}

echo “</table>”;
echo “<br />”;
?>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 25.2008 — you need some form of ID in the database(I'm assuming auto increment called 'ID'), then you add a form over the entire table and your checkboxes:
[code=php]echo "<form action='?' method='POST'>

<table style='width:100%'><tr>
<th>delete</th>
<td>Title</td>
<td>Name</td>
<td>Company</td>
<td>Address1</td>
<td>Address2</td>
<td>Town</td>
<td>County</td>
<td>Postcode</td>
</tr>";

$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<td><input type='checkbox' name='rm[]' value='{$row['ID']}' /></td>
<td>{$row['title']}</td>
<td>{$row['name']}</td>
<td>{$row['company']}</td>
<td>{$row['address1']}</td>
<td>{$row['address2']}</td>
<td>{$row['town']}</td>
<td>{$row['county']}</td>
<td>{$row['postcode']}</td>
</tr>";
}

echo "</table></form>";
echo "<br />";
?>[/code]

then a relevant delete statement:
[code=php]foreach($_POST['rm[]'] as $key => $val){// check all results
if(!is_int($val))// if the result is not a number
unset($_POST['rm'][$key]);// remove it from listings.
};
// run remove:
mysql_query("DELETE FROM tbl WHERE ID IN (".implode($_POST['rm[]'], ', ').")");[/code]
Copy linkTweet thisAlerts:
@markandkelleauthorFeb 25.2008 — Hi Thanks for your help, I think I am nearly there, so I have set up the ID in MySQL and added the checkboxes, the form action I have set as delete.php and then created this file as;

[code=php]<?
foreach($_POST['rm[]'] as $key => $val){// check all results
if(!is_int($val))// if the result is not a number
unset($_POST['rm'][$key]);// remove it from listings.
};
// run remove:
mysql_query("DELETE FROM tbl WHERE ID IN (".implode($_POST['rm[]'], ', ').")"); ?>
[/code]


however I get these errors

Warning: Invalid argument supplied for foreach() in /home/ayoocouk/public_html/mywebsite.co.uk/delete.php on line 7

Warning: implode() [function.implode]: Bad arguments. in /home/ayoocouk/public_html/mywebsite.co.uk/delete.php on line 12

any idea what I am doing wrong?

thanks again
Copy linkTweet thisAlerts:
@scragarFeb 25.2008 — [code=php]<?
if(isset($_POST['rm[]'])){
foreach($_POST['rm[]'] as $key => $val){// check all results
if(!is_int($val))// if the result is not a number
unset($_POST['rm'][$key]);// remove it from listings.
};
// run remove:
if(count($_POST['rm[]'])){
mysql_query("DELETE FROM tbl WHERE ID IN (".implode($_POST['rm[]'], ', ').")");
echo mysql_affect_rows()." results deleted.";
};
};
?>[/code]
forgot checking, sorry.

and replace tbl in the query with your table name.
Copy linkTweet thisAlerts:
@scragarFeb 25.2008 — [code=php]<?
if(isset($_POST['rm'])){
foreach($_POST['rm'] as $key => $val){// check all results
if(!is_int($val))// if the result is not a number
unset($_POST['rm'][$key]);// remove it from listings.
};
// run remove:
if(count($_POST['rm'])){
mysql_query("DELETE FROM tbl WHERE ID IN (".implode($_POST['rm'], ', ').")");
echo mysql_affect_rows()." results deleted.";
};
};
?>[/code]
after a bit of testing it works out that PHP5(and possibly PHP4) submits as an array without the [] in it's name, use the version above if you have any problems.
×

Success!

Help @markandkelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...