adamsu Posted April 28, 2011 Share Posted April 28, 2011 i need help trying to get this delete feature to work its not deleting from the database (by the way i took out my database names and passwords at the top of the file) is it possible someone could help me, ive been working on this for like a week and cant figure out the problem. thanks! you can email me at [email protected] picture 2.png is showing what it looks like <?php $host="localhost"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="database_name"; // Database name $tbl_name="table_name"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <style> /*table affects look of the whole table look */ table { margin-left: auto; margin-right: auto; border: 1px solid #330000; border-collapse:collapse; width:70%; border-width: 5px 5px 5px 5px; border-spacing: 1px; border-style: outset outset outset outset; border-color: #330000 #330000 #330000 #330000; border-collapse: separate; background-color: #330000; #800517 f535aa #330000 school color #9A0000 school color2 #991B1E school color3 #CCCC99 school color4 #9A0000 } /*th is table header */ th { text-align: left; height: 2.5em; background-color: #330000; color: #FC0; font-size:1.5em; } /*td is table data or the cells below the header*/ td { text-align: left; height:1.0em; font-size:1.0em; vertical-align:bottom; padding:10px; border-width: 5px 5px 5px 5px; padding: 8px 8px 8px 8px; border-style: outset outset outset outset; border-color: #9A0000 #9A0000 #9A0000 #9A0000; background-color: #CCCC99; -moz-border-radius: 0px 0px 0px 0px; } </style> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Pick Which Rows you want to delete, Then press delete.</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> <td align="center" bgcolor="#FFFFFF">delete</td></tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this // edited if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/235021-a-delete-script-wont-work-for-me-wont-work-for-me-need-help/ Share on other sites More sharing options...
requinix Posted April 28, 2011 Share Posted April 28, 2011 1. Use full PHP tags whenever possible. That's <?php, not the shorter . 2. Your code expects register_globals. Regardless of whether it's enabled, change your code to work without it. One or both of those could be causing your problem. 3. Don't use a (which you put in the wrong place, by the way) to do an instant redirect. Move the delete code to a place before you output anything and use header+exit; to do the redirect. 4. Make sure the ID number you're deleting is actually number. Otherwise you're open to SQL injection and that's a very bad thing. Quote Link to comment https://forums.phpfreaks.com/topic/235021-a-delete-script-wont-work-for-me-wont-work-for-me-need-help/#findComment-1207831 Share on other sites More sharing options...
Tonic-_- Posted April 28, 2011 Share Posted April 28, 2011 First off you're not defining $delete. It should be $delete = $_POST['delete']; Another thing is you are not defining $checkbox If I remember correctly about adding multiple values into one checkbox name/id Should be $checkbox = explode(",",$_POST['checkbox[]'); Code should be something similar to this I believe. $delete = $_POST['delete']; $checkbox = explode(",", $_POST['checkbox[]'); if(isset($delete)){ foreach($checkbox as $id) { $sql = "DELETE FROM $tbl_name WHERE id='{$id}'"; $result = mysql_query($sql); } } Not entirely sure as I haven't used checkboxes in the same field name for awhile. Quote Link to comment https://forums.phpfreaks.com/topic/235021-a-delete-script-wont-work-for-me-wont-work-for-me-need-help/#findComment-1207834 Share on other sites More sharing options...
PaulRyan Posted April 28, 2011 Share Posted April 28, 2011 Something like this may work, not tested so don't hold it against me. if(isSet($_POST)) { foreach($_POST['checkbox'] AS $ID) { $values[] = '\''.intval($ID).'\''; } $values = implode(' , ',$values); $myQuery = "DELETE FROM $tbl_name WHERE id IN ($values)"; if(mysql_query($myQuery)) { header('Location: delete_multiple.php'); } else { echo 'Query failed: "'.$myQuery.'"'; } } Place the above code just below the MySQL Connection details. Notice the use of $_POST, this is the correct way to retrieve posted for data. I also utilized the MySQL IN operator for the deleting part, means you only actualy do 1 query instead of multiple ones. I also added some simple sanitizing of the incoming data using intval(), this will make sure only numbers are passed to the query. Try it out, any problems post back here Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/235021-a-delete-script-wont-work-for-me-wont-work-for-me-need-help/#findComment-1207835 Share on other sites More sharing options...
adamsu Posted April 29, 2011 Author Share Posted April 29, 2011 @tonic - where would i place the your code at? Quote Link to comment https://forums.phpfreaks.com/topic/235021-a-delete-script-wont-work-for-me-wont-work-for-me-need-help/#findComment-1208296 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.