doforumda Posted August 15, 2008 Share Posted August 15, 2008 hello everyone, i wrote a php script that displays all the data from the database in a table. i put a checkbox in front of all the datas. this checkbox purpose is to delete the selected data from the database. but i dont know how to do that. i wrote a script but it deosnt work. pleaseeee help me if anybody knows about this. if you need script then let me know i ll provide the script for you thanks in advance Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted August 15, 2008 Share Posted August 15, 2008 Show us your code so we can tell you what your doing wrong. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted August 15, 2008 Share Posted August 15, 2008 I wrote up an example script for you to reference, although I didn't test it for errors. <?php //check if they pressed delete button if (isset($_POST['delete'])){ //make sure they checked something if (!empty($_POST['ids'])){ //execute delete query $query = "DELETE FROM table WHERE id IN(" .implode(', ', {$_POST['ids']}) . ")"; if ($result = mysql_query($query)) echo "Successfully Deleted"; else echo mysql_error(); } else { echo "Nothing Selected to Delete"; } } //Select info from DB $query = mysql_query("SELECT id FROM table")or die(mysql_error()); //create checkboxes echo "<form>"; while ($row = mysql_fetch_assoc($query)){ echo "<input type='checkbox' name='ids[]' value='{$row['id']}' />"; } echo "<input type='submit' name='delete' value='Delete' />"; echo "</form>"; ?> Quote Link to comment Share on other sites More sharing options...
doforumda Posted August 15, 2008 Author Share Posted August 15, 2008 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>University</title> </head> <body> <p> <?php if(!$name && !$sfield && !$sroll) { echo "Please Enter NAME or FIELD or ROLL NUMBER"; } $db = mysql_connect("localhost"); mysql_select_db("university",$db); if($name) { echo "<h1>Searched By Name:</h1>"; $query = "select * from nu where name like '%".$name."%'"; $result = mysql_query($query); echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p><b>Roll No</b></p></td> <td><p><b>Name</b></p></td> <td><p><b>Father Name</b></p></td> <td><p><b>phone No</b></p></td> <td><p><b>Address</b></p></td> <td><p><b>Field</b></p></td>"; while($record = mysql_fetch_assoc($result)) { echo "<tr> <td width= 84><input name='delete' type='checkbox' id='delete' value='yes'></td> <td width = 350><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } if($sfield) { echo "<h1>Searched By Field:</h1>"; $query = "select * from nu where field like '%".$sfield."%'"; $result = mysql_query($query); echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p><b>Roll No</b></p></td> <td><p><b>Name</b></p></td> <td><p><b>Father Name</b></p></td> <td><p><b>phone No</b></p></td> <td><p><b>Address</b></p></td> <td><p><b>Field</b></p></td>"; while($record = mysql_fetch_assoc($result)) { echo "<tr> <td width= 84><input name='checkbox' type='checkbox' id='checkbox'></td> <td width = 350><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } if($sroll) { echo "<h1>Searched By Roll Number:</h1>"; $query = "select * from nu where rollNo like ".$sroll.""; $result = mysql_query($query); echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p><b>Roll No</b></p></td> <td><p><b>Name</b></p></td> <td><p><b>Father Name</b></p></td> <td><p><b>phone No</b></p></td> <td><p><b>Address</b></p></td> <td><p><b>Field</b></p></td>"; while($record = mysql_fetch_assoc($result)) { echo "<tr> <td width= 84><input name='delete' type='checkbox' id='delete' value='yes'></td> <td><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } ?> <BR> <BR> <a href = "search.html">If you want to search more then Please Click HERE</a> <BR> <BR> <a href = "adddata_nu.html">If you want to add more then Please Click HERE</a></p> <br><br> <form name="form1" method="post" action="delete.php"> <label for="Submit"></label> <input type="submit" name="Submit" id="Submit" value="Delete"> </form> <p><BR> <BR> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
doforumda Posted August 15, 2008 Author Share Posted August 15, 2008 how to write delete script for above script pleasee help me Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted August 15, 2008 Share Posted August 15, 2008 Give this a try <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>University</title> </head> <body> <p> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php //check if they pressed delete button if (isset($_POST['submit'])){ //make sure they checked something if (!empty($_POST['delete'])){ //execute delete query $query = "DELETE FROM table WHERE id IN(" .implode(', ', {$_POST['delete']}) . ")"; if ($result = mysql_query($query)) echo "Successfully Deleted"; else echo mysql_error(); } else { echo "Nothing Selected to Delete"; } } if(!$name && !$sfield && !$sroll) { echo "Please Enter NAME or FIELD or ROLL NUMBER"; } $db = mysql_connect("localhost"); mysql_select_db("university",$db); if($name) { echo "<h1>Searched By Name:</h1>"; $query = "select * from nu where name like '%".$name."%'"; echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p>Roll No</p></td> <td><p>Name</p></td> <td><p>Father Name</p></td> <td><p>phone No</p></td> <td><p>Address</p></td> <td><p>Field</p></td>"; while($record = mysql_fetch_assoc($result)) { //Where it says $record['unique_id'], you need to change that to whatever the unique ID for the table your deleting from is echo "<tr> <td width= 84><input name='delete[]' type='checkbox' id='delete' value='{$record['unique_id']}'></td> <td width = 350><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } if($sfield) { echo "<h1>Searched By Field:</h1>"; $query = "select * from nu where field like '%".$sfield."%'"; $result = mysql_query($query); echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p>Roll No</p></td> <td><p>Name</p></td> <td><p>Father Name</p></td> <td><p>phone No</p></td> <td><p>Address</p></td> <td><p>Field</p></td>"; while($record = mysql_fetch_assoc($result)) { echo "<tr> <td width= 84><input name='checkbox' type='checkbox' id='checkbox'></td> <td width = 350><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } if($sroll) { echo "<h1>Searched By Roll Number:</h1>"; $query = "select * from nu where rollNo like ".$sroll.""; $result = mysql_query($query); echo "<table width = '100%' border = '0'"; echo "<tr bgcolor = 'lightblue'><td><P><B>Select</p></B></td> <td><p>Roll No</p></td> <td><p>Name</p></td> <td><p>Father Name</p></td> <td><p>phone No</p></td> <td><p>Address</p></td> <td><p>Field</p></td>"; while($record = mysql_fetch_assoc($result)) { echo "<tr> <td width= 84><input name='delete' type='checkbox' id='delete' value='yes'></td> <td><p>".$record["rollNo"]."</p></td> <td><p>".$record["name"]."</p></td> <td><p>".$record["fatherName"]."</p></td> <td><p>".$record["phoneNo"]."</p></td> <td><p>".$record["address"]."</p></td> <td><p>".$record["field"]."</p></td>"; } } ?> <BR> <BR> <a href = "search.html">If you want to search more then Please Click HERE[/url] <BR> <BR> <a href = "adddata_nu.html">If you want to add more then Please Click HERE[/url]</p> <label for="Submit"></label> <input type="submit" name="Submit" id="Submit" value="Delete"> </form> <p><BR> <BR> </p> </body> </html> I only did it for the if($name) statement, so make sure your testing it with that one. If that works, try to do the others yourself by looking at what I did. Quote Link to comment Share on other sites More sharing options...
doforumda Posted August 16, 2008 Author Share Posted August 16, 2008 Thank you pocobueno1388 it works thanks alot Quote Link to comment Share on other sites More sharing options...
Gighalen Posted August 16, 2008 Share Posted August 16, 2008 Just to throw my little 2 cents in, you can add this line on code in your opening FORM tag to confirm the deletion of said rows. That is, given you have a seperate FORM for each. Anyways, this will prompt you to confirm the delete to prevent you from accidently deleting something. onclick="if (confirm('Are you sure?')) {return true;} else {return false;}" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.