Jump to content

Problem insert order (and problem with deleting)


arbitter

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:ixcady.jpg

 

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.