arbitter Posted August 4, 2010 Share Posted August 4, 2010 Hello, I have a problem when inserting mysql queries (and also with the deleting) So when I insert, I use the following code: mysql_query("INSERT INTO vraag (naam,inhoud,tijd) VALUES ('$naam','$inhoud','$tijd')")or die(mysql_error()); And that inserts the code. But when I look in PHPMYADMIN, the order of all the comments is disaranged. Though I also have an ID column which is auto-insecremented. Is this normal, or am I doing it wrong? And if this is normal, I need a method to arrange the id's when displaying. Now for the deleting part: if(isset($_POST['delete'])) { foreach($_POST['id'] as $id) { mysql_select_db('blabla')or die(mysql_error()); $ontvang = mysql_query("DELETE FROM blabla WHERE id='$id'")or die(mysql_error()); $_SESSION['melding'] = 'selected items are deleted.'; header('Location: moderator.php'); exit(); } } Now the problem is, that it only deletes the highest id number if multiple are selected. Even if the higher ID is in the first position of the table in PHPMYADMIN. All the help will be highly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/ Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 What do you mean by disaranged? Can you provide an example of the data provided and the expected results versus what is actually happening? As far as the delete query goes, you have the header() and the exit() inside the foreach() loop, thereore it will redirect and exit after the first iteration. Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095099 Share on other sites More sharing options...
arbitter Posted August 4, 2010 Author Share Posted August 4, 2010 Oh my such a stupid mistake with the deleting! Thanks! And by dissaranged, I mean that sometimes in phpmyadmin the id 20 comes before id 5. Example: This is for a type of forum, and to display them I simply use $query = "SELECT naam, inhoud, tijd FROM vraag"; $result = mysql_query($query)or die(mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<div class='ctotal'> <div class='chead'> <span class='left'>" . $row['naam'] . " </span> <span class='right'>" . $row['tijd'] . "</span> </div> <div class='cc' align='center'> " . $row['inhoud'] . " </div> </div><br /><br />"; } So I don't sort them. So with the example image, the comment with id 30 would come before the comment with id 26, which is not correct, because I want the oldest first. So I guess I should sort them? Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095118 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 Yes. ORDER BY `id` ASC should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095142 Share on other sites More sharing options...
arbitter Posted August 4, 2010 Author Share Posted August 4, 2010 Could you explain a little better how to do that? I'm quite new to all of these things... Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095196 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 You'd use it when selecting the records for display. SELECT `field1`, `field2` FROM `table` WHERE `id` = $value ORDER BY `id` ASC Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095205 Share on other sites More sharing options...
arbitter Posted August 4, 2010 Author Share Posted August 4, 2010 Oh wow it works! So this is what I did, and it works like a charm: $query = "SELECT naam, inhoud, tijd FROM vraag ORDER BY id ASC"; $result = mysql_query($query)or die(mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {whatever;} Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/209785-problem-insert-order-and-problem-with-deleting/#findComment-1095324 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.