Hi,
How do I display all records from the database, along with a check box?
Upon a submit button being clicked, how do I delete selected records?
| SitePoint Sponsor |


Hi,
How do I display all records from the database, along with a check box?
Upon a submit button being clicked, how do I delete selected records?





Put the numeric key for each record in the value attribute of the checkbox. Name the checkboxes as a group using array syntax and then loop through the array on post and delete each record that has an id in the array.
Code:<input type="checkbox" name="delgroup[0]" value="215" /> <input type="checkbox" name="delgroup[1]" value="217" /> <!-- etc. etc. -->PHP Code:if(!empty($_POST['delgroup']) && is_array($_POST['delgroup']))
{
//connect to db blah blah blah
for($i = 0;$i < count($_POST['delgroup']);$i++)
{
$sql = 'DELETE FROM whatevertable WHERE id='.intval($_POST['delgroup'][$i]);
mysql_query($sql);
}
}


Hammer65,
I found the following code within the forums, how do I merge you code into it, resulting in records being deleted?
Many Thanks in Advance!!! - Much Appreciated!!!Code:<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM person"); while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo $row['LastName']; } mysql_close($con); ?>





This assumes that each record has a unique integer id (which it should). Put the following within the form code.
Use the previous code for the submission.PHP Code:$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
// in development okay not production
die('Could not connect: ' . mysql_error());
// for production
// echo('<h2>Sorry!</h2><p>We are having technical difficulties</p>');
}
else
{
mysql_select_db("my_db", $con);
if($result = mysql_query("SELECT id,concat(FirstName,' ',LastName) as name FROM person"))
{
$i = 0;
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo '<label>';
echo '<input type="checkbox" name="delgroup['.$i.']" value="'.$row['FirstName'].'" />'."\n";
echo $row['name'].'</label>';
$i++;
}
}
else
{
echo '<h2>Sorry!</h2><p>No Records Found.</p>';
}
}
else
{
// in development okay not production
die('Query failed: ' . mysql_error());
// for production
// echo('<h2>Sorry!</h2><p>We are having technical difficulties</p>');
}
mysql_close($con);
}


How do I go about doing that?Use the previous code for the submission.
Many Thanks,





Put the form and the checkbox code on one php page and the submission code on another. Set the form to post to the page with the submission code on it. Look for a few PHP tutorials on this sort of thing. I can't write all the code for you.


How do I do it so it doesn't all the processing on the same page, instead of switch pages?





Some people put a name attribute on the submit button, which will cause the browser to send the text that is in the submit button's value attribute as part of the form data. They then check for the presence of that value in the $_POST or $_GET arrays (depending on what method your form is using) and then execute the appropiate code.
If it's present then run the submission code, if it isn't then it should run the form code.


Everyone,
Is there a better and/or shorter way of coding the following?
Is there anyway records can be deleted using check boxes, with having to need arrays?
Many Thank - Any Help, Much Appreciated!Code:<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("my_db", $con); if(isset($_POST['delete'])) { if(is_array($_POST['id'])) { foreach($_POST['id'] as $id) { $list=((int)$id).','; } $list=substr($list,0,-1); } else { $list=(int)$_POST['id']; } $query="delete from person where id in ($list)"; mysql_query($query) or die(mysql_error()); } $result = mysql_query("SELECT * FROM person"); if(mysql_error() != 0 || mysql_num_rows($result) == 0) { die("No database, or no data"); } echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'; while($row = mysql_fetch_object($result)) { echo $row->FirstName . "<br>"; echo $row->LastFirstName . "<br>"; echo $row->Age . "<br>"; echo "<input FirstName='id[]' type='checkbox' value='".$row->id."'><br>"; } echo "<input type='submit' FirstName='delete' value='Delete'><br></form>"; ?>![]()
Bookmarks