fife Posted January 31, 2012 Share Posted January 31, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/ Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 because you are using !== instead of != Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312942 Share on other sites More sharing options...
fife Posted January 31, 2012 Author Share Posted January 31, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312945 Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 then $row['image2'] is not empty, without seeing anymore relevant code, I can't help any further. Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312947 Share on other sites More sharing options...
fife Posted January 31, 2012 Author Share Posted January 31, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312950 Share on other sites More sharing options...
fife Posted January 31, 2012 Author Share Posted January 31, 2012 ok now i see the error and my code makes no sense. Other than creating an images table separate from the posts table and storing up to 5 in there per post is there any way of fixing this? Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312956 Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/256117-executing-certain-code-based-on-outcome/#findComment-1312959 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.