Jump to content

mysql_insert_id()


The Little Guy

Recommended Posts

OK, the following code is in a foreach loop, and I can not figure out why it isn't working. Basically all it does is build a string to pass through the URL to the next page.

 

The string to be created should look like this:

imageID=148&imageID2=149

But looks like this:

imageID=148&imageID2=148

 

Notice in the second one the 2 numbers are the same, where in the first one they are different. My code creates the result of the second string, but I would rather the first string. Why is it doing this?

 

<?php
$twoUp = FALSE;
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        // upload to server and create thumbs
        mysql_query("INSERT INTO guest_images (`directory`,`file`) VALUES ('$savelocation','$saveFile')");
        if(!$twoUp){
            $imID1 = '?imageID='.mysql_insert_id();
            if(empty($_FILES['file']['name'][1])){
                break;
            }else{
                $twoUp = TRUE;
            }
        }
        if($twoUp){
            $imID2 = '&imageID2='.mysql_insert_id();
        }else{
            $imID2 = '';
        }
    }
}
header("Location: guestImage.php".$imID1.$imID2);
exit;
?>

Link to comment
https://forums.phpfreaks.com/topic/90921-mysql_insert_id/
Share on other sites

But since it is in a loop, it should work shouldn't it? what happens, is that it goes into the first if statement and sets $imID1 equal to: ?imageID=148 (or some other number). then it starts the loop over and goes into the second if and sets $imID2 equal to: ?imageID=149 (or some other number). But then $imID1 also changes for some reason, even though it never goes back into that first if statement, since I echo out some random word, and it only echo's out one time.

Link to comment
https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-465988
Share on other sites

Oh yeah I have also tried this:

 

<?php
$twoUp = FALSE;
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        // upload to server and create thumbs
        mysql_query("INSERT INTO guest_images (`directory`,`file`) VALUES ('$savelocation','$saveFile')");

$insertID = mysql_insert_id();

if(!$twoUp){
	$imID1 = '?imageID='.$insertID;
	if(empty($_FILES['file']['name'][1])){
		break;
	}else{
		$twoUp = TRUE;
	}
}
if($twoUp){
	$imID2 = '&imageID2='.$insertID;
	break;
}else{
	$imID2 = '';
	break;
}
    }
}
header("Location: guestImage.php".$imID1.$imID2);
exit;
?>

Link to comment
https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-465996
Share on other sites

As far as I can tell, its only going to execute the loop one time. You are using 'foreach', but then only passing it one value. If that value is equal to there being no error, it enters a value into your database, giving you the value contained in mysql_insert_id().

 

Since $twoUp is set to false, it enters the next if statement, and sets the value for imageID. Then, it goes onto the next statement, and since th file has a name, it sets $twoUP to true. It then goes on to the next if statement, and since $twoUp is true, it then sets imageID2 with the same number.

 

I could be wrong about the number of times the loop is being executed, but I'm correct about the rest of your code. You are setting $twoUp to true each time after setting the first imageID, which is where your problem is coming from.

Link to comment
https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-466001
Share on other sites

There! I reversed the two, and removed the breaks, and it FINALLY works.

<?php
if($twoUp){
$imID2 = '&imageID2='.$insertID;
}else{
$imID2 = '';
}
if(!$twoUp){
$imID1 = '?imageID='.$insertID;
if(empty($_FILES['file']['name'][1])){
	continue;
}else{
	$twoUp = TRUE;
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-466021
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.