By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Delete records via checkbox

P: 1
I'm just learning PHP (extreme novice) and I'm struggling with deleting records using a checkbox included within each row.

I have a few idea but I'm not sure if my thoughts are correct. I think I need to turn it into a Form to be able to capture whats been checked but I would appreciate any guidance.

Most of the code I am using is from a solution a co-worker wrote for another solution and I learning by using it as a guide/example if some of it look advanced for an extreme novice.

Thanks in advance.

-------------------- Code showing to show the list ---------------------------

<?php
include_once('dAud_connect.php');
$dbh=mysql_connect($host,$user,$pass);
if (!$dbh)
{
echo "could not connect to database";
exit;
}
mysql_select_db($db);


{
$query = "select firstName, middleName, lastName, address1, address2, city, state, zip_code, country, phone, email, gender, citizenship, program, DATE_FORMAT(submitDate, '%m/%d/%Y') as submitDate, DATE_FORMAT(auditionDate, '%m/%d/%Y') as auditionDate from d_Aud";
}

$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($result)
{
echo "<table border=1>";
echo "<tr><th>firstName</th><th>middleName</th><th>lastName</th><th>Address1</th><th>Address2</th><th>city</th><th>state</th><th>zip</th><th>Country</th><th>phne</th><th>Email</th><th>Gender</th><th>Citizenship</th><th>Program</th><th>Audition Date</th><th>DateSubmitted</th><th>Delete</th></tr>";
while ($get_info = mysql_fetch_array($result))
{
echo "<tr>\n";
echo "\t<td>".stripslashes($get_info['firstName'])."</td>";
echo "\t<td>".stripslashes($get_info['middleName'])."</td>";
echo "\t<td>".stripslashes($get_info['lastName'])."</td>";
echo "\t<td>".stripslashes($get_info['address1'])."</td>";
echo "\t<td>".stripslashes($get_info['address2'])."</td>";
echo "\t<td>".stripslashes($get_info['city'])."</td>";
echo "\t<td>".stripslashes($get_info['state'])."</td>";
echo "\t<td>".stripslashes($get_info['zip_code'])."</td>";
echo "\t<td>".stripslashes($get_info['country'])."</td>";
echo "\t<td>".stripslashes($get_info['phone'])."</td>";
echo "\t<td>".stripslashes($get_info['email'])."</td>";
echo "\t<td>".stripslashes($get_info['gender'])."</td>";
echo "\t<td>".stripslashes($get_info['citizenship'])."</td>";
echo "\t<td>".stripslashes($get_info['program'])."</td>";
echo "\t<td>".stripslashes($get_info['auditionDate'])."</td>";
echo "\t<td>".stripslashes($get_info['submitDate'])."</td>";
echo "\t<td><input type='checkbox' name='del_me[]' value=\"$url\"></td>";
echo "</tr>\n";
}
echo "</table>";
}
echo mysql_error();
echo "<br />".$num_rows." records";
echo "<br /><a href='../php_inc/dAud_excel.php'>get as excel file</a>";
echo "<br /><a href='../php_inc/delete_entry.php'>delete check</a>";
?>



------------------ Code to Delete Rows ----------------------------

<?php
include_once('../php_inc/dAud_connect.php');
$dbh=mysql_connect($host,$user,$pass);
if (!$dbh)
{
echo "could not connect to database";
exit;
}

mysql_select_db($db);

$del_me = $HTTP_POST_VARS['del_me'];
$valid_user = $HTTP_SESSION_VARS['valid_user'];



if (count($del_me) >0)
{
foreach($del_me as $url)
{
if (delete_bm($valid_user, $url))
echo 'Deleted '.htmlspecialchars($url).'.<br />';
else
echo 'Could not delete '.htmlspecialchars($url).'.<br />';
}
}

?>
Oct 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: 50
Well i am a little confused by :
Expand|Select|Wrap|Line Numbers
  1. echo "\t<td><input type='checkbox' name='del_me[]' value=\"$url\"></td>";
What is the deal with the bracket -> del_me[]
And \"$url\" Should be something like $get_info['userID']

Or whatever as long as "userID" is a unique identifier to the row which shall be deleted.
When the user checks the button - do you envision the row deleting right then, the page refreshing and that row being gone????
If so, you will need to look into using something like attaching
Expand|Select|Wrap|Line Numbers
  1. onChange="submit()"
to each checkbox input....submit() being a javascript function that you created in the document header, that does at least:
Expand|Select|Wrap|Line Numbers
  1. function submit(){
  2. document.form.submit;}
Finally the hypothetical 'userID' will serve as the clause for a WHERE parameter in your delete statement....

Hope this helps!
Oct 18 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.