Forum Moderators: coopster
Example....
I have 5 records and i want to delete two. So i'll check 2 checkboxes and then "submit" it. how do i do this in php? making sure that the records were really deleted in my database.
// Basic example
// remember this is very basic and does not address security concern
// authentify this script user
// verify that each input is really what it is supposed to be
// do a two steps, when submitted add an itermediary script to ask "Are you sure? you are about to delete etc..."
<tr>
<td width='24%'><div id="h3">Username:<br> <br></div></td>
<td width='24%'><div id="h3">First name:<br> <br></div></td>
<td width='24%'><div id="h3">Last name:<br> <br></div></td>
<td width='24%'><div id="h3">Select:<br> <br></div></td>
</tr>
<?php
$conn = db_connect();
$query = "select username, first_name, last_name FROM #*$!#*$! WHERE userlevel='aaaaa' ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['username'];
echo "</td><td>";
echo $row['first_name'];
echo "</td><td>";
echo $row['last_name'];
// used for checkbox
$username=$row['username'];echo"$username";
// ends used for checkbox
echo"<td width=\"30%\"> <input type=\"checkbox\" name=\"username\" value=$username></td>";
echo "</td></tr>";
}
echo "
<tr><td colspan=\"4\" align=\"right\">
<input type=\"submit\" value=\"Select a user/client\" /></td></tr></table>
</div>
</form>
</body>
</html>";
}
the above is the form then it goes to a second script:
<?php
$username=$_POST['username'];
$conn = db_connect();
$query = "DELETE FROM #*$!XX WHERE username='$username' AND userlevel='aaaaa' ";
$result = mysql_query($query);
if($result)
echo"<h1>Done!</h1>
<a href=\"main.php\">Back to main CP</a>";
?>
The 'name' field should always be the same, and terminate with [] so that entries go into a PHP array; the 'value' field should then identify which was chosen.
<input type="checkbox" name="choice[]" value="1" />
<input type="checkbox" name="choice[]" value="2" />
<input type="checkbox" name="choice[]" value="3" />
<input type="checkbox" name="choice[]" value="4" />
<input type="checkbox" name="choice[]" value="5" />
After submission, in PHP, you then access the array as $_POST['choice'] (or indeed $_GET['choice'] if you used the GET method for your form), a 2D array with values equal to the checkboxes you selected.
To do as you suggested and delete those checked records, you must loop through the array, and run a delete for each one you find:
foreach ($_POST['choice'] as $chosenoption) mysql_query("DELETE FROM `yourtable` WHERE `optionid` = ".intval($chosenoption).""); Note the use of 'intval()' to ensure that the passed value was an integer; and indeed the good practice of only having integers as values in this kind of system - relating of course to the unique ID for the table.
Were you to select rows to delete based upon a string (e.g. 'username') then you would have to use the function mysql_real_escape_string() to ensure its safety, and enclose that resulting string with ' ' marks.
<html>
<body link='000000' vlink='000000' alink='gray' >
<table border='0' width='100%' align='center' bgcolor='ffffff' >
<tr><td></br></td></tr>
<tr>
<td width='25%' valign='bottom'>
<table border='0'>
<tr><td><font face='tahoma' size='4'><a href='home.php' style='text-decoration:none;'>Home</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='createEmp.php' style='text-decoration:none;'>Create Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='search_view5.php' style='text-decoration:none;'>Edit Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4'color='silver'>Delete Personnel Record</font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='search_view3.php' style='text-decoration:none;'>View Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4'><a href='sign.php' style='text-decoration:none;'>Logout</a></font></td></tr>
</table>
</td>
<td><img src='header5.jpg' align='center'/></td>
</tr>
<tr>
<td colspan='2' background='line.jpg' height='8'></td>
</tr>
<tr>
<td colspan='2' background='line2.jpg' height='2'></td>
</tr>
<tr>
<td colspan='2' height='20' valign='top'>
</br> <font face='tahoma' size='5' color='gray'>Search Patient Profile</font>
</br><hr width='30%' align='left' size='3' color='silver'></hr></br></br></br>
<center>
<form action="search_view6.php" method="post" >
<input type="submit" value="choice" />
<input type="text" size='40' maxLength="50" name="choice" />
<select name='choose' >
<option value='name'>Lastname</option>
<option value='id'>Doctor's License</option>
</select>
</form>
</center>
</tr>
<tr>
<td colspan='2' height='220' valign='top'>
<?php
$con=mysql_connect("localhost", "root", "x");
if(!$con)
{
die('could not connect: '. mysql_error());
}
mysql_select_db("clinic", $con);
if($_POST[choice]!=NULL)
{
$_SESSION['var'] = $_POST[choice];
$result=mysql_query("SELECT doc_license, doc_fname, doc_mname, doc_lname FROM doctor where doc_license= '$_POST[choice]' or doc_lname= '$_POST[choice]' ");
$row =mysql_fetch_assoc($result);
if($row!=NULL)
{
echo "<table border='5' align='center' bordercolor='skyblue' bordercolorlight='skyblue' bordercolordark='skyblue' cellpadding='2' cellspacing='0' width='50%'>
<tr>
<th>Doctor's License</th>
<th>Name</th>
<th>Delete</th>
</tr>";
echo "<tr align='center'>";
echo "<td>". $row['doc_license']."</td>";
echo "<td>". $row['doc_fname']." ". $row['doc_mname']." ". $row['doc_lname']."</td>";
echo "<td> <input type=\"checkbox\" name=\"id" . $row[doc_license] . "\" value=\"1\"></td>";
$idList = $sep.$row[id];
$sep = ",";
echo "</tr>";
echo "</table>";
echo "<form action='deleteRec.php' method='post' >
<input type='submit' value='Delete Personnel Record'/>
</form>";
echo "<input type=\"hidden\" name=\"idlist\" value=\"$idList\">";
}
else
{
echo "<center>Sorry, record does not exist...</center>";
}
}
mysql_close($con);
?>
</tr>
<tr>
<td></br></br></br></br></td>
</tr>
<tr>
<td colspan='2' background='line2.jpg' height='2'></td>
</tr>
</table>
</body>
</html>
..........................................PHP..................
<?php
session_start();
$idlist=$_POST['id'];
$con=mysql_connect("localhost", "root", "x");
if(!$con)
{
die('could not connect: '. mysql_error());
}
mysql_select_db("clinic", $con)or die('could not connect: '. mysql_error());
$array_id = explode("," $idlist");
foreach($array_id as $id)
{
if(${"id" . $id} == 1)
{
$result=mysql_query("DELETE * FROM doctor where doc_license= $id ");
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Patient Record</title>
</head>
<body link='000000' vlink='000000' alink='gray'>
<table border='0' width='100%' align='right' bgcolor='ffffff'>
<tr><td></br></td></tr>
<tr>
<td width='25%' valign='bottom'>
<table border='0'>
<tr><td><font face='tahoma' size='4'><a href='home.php' style='text-decoration:none;'>Home</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='createEmp.php' style='text-decoration:none;'>Create Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='search_view5.php' style='text-decoration:none;'>Edit Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4'color='silver'>Delete Personnel Record</font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4' ><a href='search_view3.php' style='text-decoration:none;'>View Personnel Record</a></font></td></tr>
<tr><td background='line2.jpg' height='3'></td></tr>
<tr><td><font face='tahoma' size='4'><a href='sign.php' style='text-decoration:none;'>Logout</a></font></td></tr>
</table>
</td>
<td><img src='header5.jpg' align='center'/></td>
</tr>
<tr><td height='10'></td></tr>
<tr>
<td colspan='2' background='line.jpg' height='8'></td>
</tr>
<tr>
<td colspan='2' background='line2.jpg' height='2'></td>
</tr>
<tr>
<td colspan='2' height='220' valign='top'>
</br>
<font face='tahoma' size='5' color='gray'>Record Has Been Deleted!</font>
<br>
</td>
</tr>
<tr>
<td></br></br></br></br></td>
</tr>
<tr>
<td colspan='2' background='line2.jpg' height='2'></td>
</tr>
</table>
</body>
</html>
what's wrong with this?...when i run it it generates an error pertaining to this
$array_id = explode("," $idlist");
i think i haven't accessed it well from my form...what do you think i should do?
Also, you need exactly one <form> tag, at the start of your fields, and termination with one </form> tag after every single field.