Jump to content

TRUE/FALSE or MySQL issue?


The Little Guy

Recommended Posts

OK, the problem is where I create the $imID and the $imID2. Where it says $row['id'];, it is placing the last number in the database... which it is what I want, but why Is it placing the same id in both $row['id'];?

 

as you may notice, if $twoUp is false, $imID2 should not be set. but if it is true, then $imID2 should be set.

 

<?php
$twoUp = FALSE;
foreach ($_FILES["file"]["error"] as $key => $error) {
//Some more PHP Here
//Start of problem
mysql_query("INSERT INTO guest_images (`directory`,`file`) VALUES ('$savelocation','$saveFile')");
$sql = mysql_query("SELECT id FROM guest_images WHERE `directory`='$savelocation' AND `file`='$saveFile' LIMIT 1");
$row = mysql_fetch_array($sql);
if($twoUp){
	$imID2 = '&imageID2='.$row['id'];
}else{
	$imID2 = '';
}
if(!$twoUP){
	$imID1 = '?imageID='.$row['id'];
	if(empty($_FILES['file']['name'][1])){
		$twoUp = FALSE;
		break;
	}else{
		$twoUp = TRUE;
	}
}
}
?>

Link to comment
Share on other sites

Can you give a concrete example about what goes in, what you expect to happen (with real values as examples), and then what actually happens?

 

I am not sure I understand the problem exactly from your description, but I am usually pretty good at catching bugs like this.  Also, make sure all the loop conditionals, etc., are doing what you expect them to.  Sometimes that is the issue.

Link to comment
Share on other sites

OK... The example:

 

on the home page, the user has two file boxes, so he/she can upload one or two images. next they press upload.

 

$_FILES['file']['name'][1] contains the file name of the image such as: flowers.jpg

When It goes through the loop once, $twoUp is set to false. when It gets to the if/else statement (if(empty($_FILES['file']['name'][1])){) that checks to see if there is a second image, if there is, then it sets $twoUp to true, otherwise it sets it to false.

 

That works all fine and dandy, but where it says: $row['id'] it will set them BOTH to the very last ID value in the table (a.ka. the largest value).

 

So...

this an example result I am getting:

$imID1 = '?imageID=23';

$imID2 = '&imageID2=23';

 

And this is an example result I am expecting:

$imID1 = '?imageID=22';

$imID2 = '&imageID2=23';

 

I think that covers it all...

Link to comment
Share on other sites

Okay, I see.  If that is the case, the code we would need to see is probably above that.  I would say to spit out the query as text for each one so you can see what you are doing.  You are probably not changing the $savefile variable correctly, and it is asking for the same file twice.  If so, then the code is simply doing what you are asking it to do, but the logic is wrong. 

 

You can also try running them through phpMyAdmin to get a better handle on what is happening.

 

If that does not work, make sure to dump the $_FILES array and to make sure the upload is what you expect.  It could be a form error, but I would try the php stuff first.

 

There is also the mysql_insert_id function.  If you are not using that, it might help as well, assuming that the id column is a primary key auto_increment field.

 

Let me know what you find.

Link to comment
Share on other sites

OK... Here is the entire page:

<?php
include"db.php";
session_start();
$a = 1;
$twoUp = FALSE;
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
	$tmp_name = $_FILES["file"]["tmp_name"][$key];
	$name = $_FILES["file"]["name"][$key];
	$file = getext($name);
	$time = time();
	$string = "abcdefghijklmnopqrstuvwxyz0123456789";
	$randnum = '';
	for($i=0;$i<25;$i++){
		$pos = rand(0,36);
		$randnum .= $string{$pos};
	}
	$dircnt=0;
	$dirarr = array();
	foreach(ListDescendantDirectories('guest_images') as $dir){
		if(preg_match("~/thumbs~",$dir)){
			continue;
		}else{
			array_push($dirarr,$dir);
			$dircnt++;
		}
	}
	$filee = array_rand($dirarr);
	$savelocation = $dirarr[$filee];

	#1 equals overwrite
	$guestTypes = array(".jpg",".gif",".png");
	if(in_array($file,$guestTypes)){
		move_uploaded_file($tmp_name, $savelocation."/".$time.$randnum.$name);
		createThumbnail($savelocation, $time.$randnum.$name, $savelocation.'/thumbs', 120, 80);
		$_SESSION['guestfile'.$a] = "http://tzfiles.com/$savelocation/".$time.$randnum.$name;
		$_SESSION['guestfile'.$a.'error'] = 1;
		$saveFile = $time . $randnum . $name;
		mysql_query("INSERT INTO guest_images (`directory`,`file`) VALUES ('$savelocation','$saveFile')");
		$sql = mysql_query("SELECT id FROM guest_images WHERE `directory`='$savelocation' AND `file`='$saveFile' LIMIT 1");
		$row = mysql_fetch_array($sql);
		if($twoUp){
			$imID2 = '&imageID2='.$row['id'];
		}else{
			$imID2 = '';
		}
		if(!$twoUP){
			$imID1 = '?imageID='.$row['id'];
			if(empty($_FILES['file']['name'][1])){
				$twoUp = FALSE;
				break;
			}else{
				$twoUp = TRUE;
			}
		}
	}else{
		$_SESSION['guestfile'.$a.'error'] = 2;
		if(!in_array($file,$guestTypes)){
			$_SESSION['guestfile'.$a.'error_txt'] .= '<span style="color:red">Incorrect File Type .jpg, .gif, .png only</span>';
		}
	}
}
$a++;
}
header("Location: guestImage.php".$imID1.$imID2);
exit;
?>

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.