The Little Guy Posted February 13, 2008 Share Posted February 13, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/ Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 mysql_insert_id() returns the id number of the last row to be inserted into the database from your last query. You are using that twice, so its the same number both times. Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-465982 Share on other sites More sharing options...
The Little Guy Posted February 13, 2008 Author Share Posted February 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-465988 Share on other sites More sharing options...
The Little Guy Posted February 13, 2008 Author Share Posted February 13, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-465996 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-466001 Share on other sites More sharing options...
The Little Guy Posted February 13, 2008 Author Share Posted February 13, 2008 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-466021 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/90921-mysql_insert_id/#findComment-466024 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.