beam1985 Posted March 9, 2008 Share Posted March 9, 2008 After a good 24 hours of work on this code, ive finally gotten rid of all the error messages, but the page doesnt work yet, it doesn't correctly delete any rows. Perhaps someone sees something in the code that i dont? thank you so much! heres the code in action(or inaction) http://iam.colum.edu/students/Robert.Agra/delete.php and heres the code <?php $host="localhost"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("ragra"); if (!isset($_POST['delete'])){ $sql="SELECT * FROM article"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table> <tr> <td><img src="comcarblog.jpg"></td> </tr> </table> <table> <tr> <td><a href="main.php" alt="Home"><img src="home.jpg"></a></td> <td><a href="" alt=""><img src="blank.jpg"></a></td> <td><a href="add.php" alt="Admin"><img src="admin.jpg"></a></td> </tr> </table> <form name="form1" method="post"> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td colspan="4" bgcolor="#41a1f3"> You're about to delete some posts! </td> </tr> </table> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center" bgcolor="#f3d841">Number</td> <td align="center" bgcolor="#f3d841">Id</td> <td align="center" bgcolor="#f3d841">Title</td> <td align="center" bgcolor="#f3d841">Tagline</td> <td align="center" bgcolor="#f3d841">Author</td> </tr> <?php while($rows=mysql_fetch_array($result)) { ?> <tr> <td align="center" bgcolor="#FFFFFF"> <input type="checkbox" name="checkbox[]" /> <input type="hidden" value="<?php echo $rows['id']; ?>" name="itemid[]" /> </td> <td bgcolor="#FFFFFF"> <?php echo $rows['id']; ?> </td> <td bgcolor="#FFFFFF"> <?php echo $rows['title']; ?> </td> <td bgcolor="#FFFFFF"> <?php echo $rows['tagline']; ?> </td> <td bgcolor="#FFFFFF"> <?php echo $rows['author']; ?> </td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"> <input name="delete" type="submit" id="delete" value="Delete"> </td> </tr> </table> </form> <?php } else { if($_POST['delete'] == 'Delete'){ for($i=0;$i<count($_POST['itemid']);$i++) { if(isset($_POST['checkbox'][$i]) && $_POST['checkbox'][$i]=='checked') { $del_id = $_POST['itemid'][$i]; $sql = "DELETE FROM article WHERE id='$del_id'"; $result = mysql_query($sql); } } header("Location:delete.php"); } } mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2008 Share Posted March 9, 2008 see http://www.phpfreaks.com/forums/index.php/topic,186314.msg834751.html#msg834751 Quote Link to comment Share on other sites More sharing options...
beam1985 Posted March 9, 2008 Author Share Posted March 9, 2008 see http://www.phpfreaks.com/forums/index.php/topic,186314.msg834751.html#msg834751 which part of the above link should i use? how does it work with my code? Quote Link to comment Share on other sites More sharing options...
beam1985 Posted March 9, 2008 Author Share Posted March 9, 2008 im not using javascript unlike the mentioned thread Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2008 Share Posted March 9, 2008 which part of the above link should i use? how does it work with my code? see http://www.phpfreaks.com/forums/index.php/topic,186314.msg834751.html#msg834751 The particular post of mine that the link points to If the checkboxes have the id as the value <?php echo '<input type="checkbox" name="chk[]" value="{$news['id']}" />'; ?> then you can do all the deletions with a single query <?php $deletes = join(',', $_POST['chk'] ); $query = mysql_query("DELETE FROM site_news WHERE id IN ($deletes) "); Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2008 Share Posted March 9, 2008 Glad it worked for you. Your code ignored the fact that only checked checkboxes are posted and so assumed that the i'th posted checkbox would be map to the i'th posted record id . You can check for a checkbox being checked on the client (javascript) but not in PHP on the server. 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.