Jump to content

Recommended Posts

Hi All

 

Basically I have 4 fields in my database

 

image2, image3, image4, image5

 

I have an image uploader to upload the files but I need what I need is if image2 is empty put in image3, if image3 is empty put in image4

 

so far I have this

 

//so first is the query to find the right row which gives the variable $row

//now here is what im doing

if($row['image2']!==""){
//run this code
}

elseif($row['image3']!==""){
//run this code
}

elseif($row['image4']!==""){
//run this code
}

elseif($row['image5']!==""){
//run this code
}

 

Now the problem I'm having is the code that is always executing is the first one of $row['image2']

 

even if image2 has a value in the database it still runs that code and over writes the field in the database. It never moves on to field 3 or 4.  Can someone please tell me if I've done something wrong.

 

 

I'm not storing the image in the database just the name.

 

Thanks

 

Danny

Link to comment
https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/
Share on other sites

ok ive changed the code so it reads...

if($row['image2']!=""){
//run this code
}

elseif($row['image3']!=""){
//run this code
}

elseif($row['image4']!=""){
//run this code
}

elseif($row['image5']!=""){
//run this code
}

 

but its still only uses the image2 field

 

I have even tried

 

if($row['image2']!=""){
//run this code
}

if($row['image3']!=""){
//run this code
}

if($row['image4']!=""){
//run this code
}

if($row['image5']!=""){
//run this code
}



 

but that doesnt work either

ok yes

 

right im trying to check if image2 is empty if it is run the code if its not leave it and check image3, if its empty run the code if its not leave it and so on.

 

$cityq = mysql_query(SELECT * from cities WHERE citiesID = '$city');
$city = mysql_fetch_array($cityq);

[code=php:0]

if($city ['image2']!=""){
//add picture and update field image2
$fileName9 = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];	

$randName = md5(rand() * time());
$fileName9 = $randName.$fileName9;
$limit_size=2097152;
$folder = "{$_SERVER['DOCUMENT_ROOT']}/images/memberImages/";


if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$success=0;}	

elseif ($fileName9=="") {$error = "Please choose a file to upload"; $success=0;}

//elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";}

$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');

if (in_array($fileType, $types)) {

if(move_uploaded_file($tmpName , $folder.$fileName9)) {

$Fnew1 =  $fileName9;
$cityname = $_POST['city'];
$post =mysql_real_escape_string($city['postID']);
$imageName = mysql_real_escape_string($_POST['imageName']);

$UpdateImageq = mysql_query("UPDATE posts SET image2 ='$Fnew1', imageName2 = '$imageName' WHERE postID ='$post' ") or die('error 2');

$url = "/view.php?city=$cityname";  
header("Location: $url");   

}
}
else{echo"Move fail2";  }
//end add picture
}






// now if image2 is full run this code

if($city['image3']!="") {
//add picture
$fileName9 = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];	

$randName = md5(rand() * time());
$fileName9 = $randName.$fileName9;
$limit_size=2097152;
$folder = "{$_SERVER['DOCUMENT_ROOT']}/images/memberImages/";


if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$success=0;}	

elseif ($fileName9=="") {$error = "Please choose a file to upload"; $success=0;}

//elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";}

$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');

if (in_array($fileType, $types)) {

if(move_uploaded_file($tmpName , $folder.$fileName9)) {

$Fnew1 =  $fileName9;
$cityname = $_POST['city'];
$post =mysql_real_escape_string($city['postID']);
$imageName = mysql_real_escape_string($_POST['imageName']);

$UpdateImageq = mysql_query("UPDATE posts SET image3 ='$Fnew1', imageName3 = '$imageName' WHERE postID ='$post' ") or die('error 3');

$url = "/view.php?city=$cityname";  
header("Location: $url");   

}
}
else{echo"Move fail3";  }	
}

 

the user can only upload 5 photos in total so the script repeats 4 times as the 1st image is uploaded else where

ok yes

 

right im trying to check if image2 is empty if it is run the code if its not leave it and check image3, if its empty run the code if its not leave it and so on.

 

$cityq = mysql_query(SELECT * from cities WHERE citiesID = '$city');
$city = mysql_fetch_array($cityq);

[code=php:0]

if($city ['image2']!=""){
//add picture and update field image2
$fileName9 = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];	

$randName = md5(rand() * time());
$fileName9 = $randName.$fileName9;
$limit_size=2097152;
$folder = "{$_SERVER['DOCUMENT_ROOT']}/images/memberImages/";


if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$success=0;}	

elseif ($fileName9=="") {$error = "Please choose a file to upload"; $success=0;}

//elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";}

$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');

if (in_array($fileType, $types)) {

if(move_uploaded_file($tmpName , $folder.$fileName9)) {

$Fnew1 =  $fileName9;
$cityname = $_POST['city'];
$post =mysql_real_escape_string($city['postID']);
$imageName = mysql_real_escape_string($_POST['imageName']);

$UpdateImageq = mysql_query("UPDATE posts SET image2 ='$Fnew1', imageName2 = '$imageName' WHERE postID ='$post' ") or die('error 2');

$url = "/view.php?city=$cityname";  
header("Location: $url");   

}
}
else{echo"Move fail2";  }
//end add picture
}






// now if image2 is full run this code

if($city['image3']!="") {
//add picture
$fileName9 = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];	

$randName = md5(rand() * time());
$fileName9 = $randName.$fileName9;
$limit_size=2097152;
$folder = "{$_SERVER['DOCUMENT_ROOT']}/images/memberImages/";


if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$success=0;}	

elseif ($fileName9=="") {$error = "Please choose a file to upload"; $success=0;}

//elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";}

$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');

if (in_array($fileType, $types)) {

if(move_uploaded_file($tmpName , $folder.$fileName9)) {

$Fnew1 =  $fileName9;
$cityname = $_POST['city'];
$post =mysql_real_escape_string($city['postID']);
$imageName = mysql_real_escape_string($_POST['imageName']);

$UpdateImageq = mysql_query("UPDATE posts SET image3 ='$Fnew1', imageName3 = '$imageName' WHERE postID ='$post' ") or die('error 3');

$url = "/view.php?city=$cityname";  
header("Location: $url");   

}
}
else{echo"Move fail3";  }	
}

 

the user can only upload 5 photos in total so the script repeats 4 times as the 1st image is uploaded else where

 

if you want a code block to run if the image is empty, then your conditions are the opposite of what they should be:

 

if(empty($row['image2']))
{
//run this code
}else
{
//move along to another image condition
}

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.