dark22horse Posted May 20, 2007 Share Posted May 20, 2007 If $photo = error4 (no file has been uploaded) Run the script that doesnt include the upload files else Run the script that includes the upload file. I have attempted the script below, but it doesnt work. <? require_once("mysql_config.php"); $target_path = $target_path . basename( $_FILES['photo']['name']); $_FILES['photo']['tmp_name']; $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['photo']['name']); if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) { $id = $_POST['id']; $price = $_POST['price']; $description = $_POST['description']; $photo = $_FILES['photo']['name']; $photo1 = $_FILES['photo1']['name']; $photo2 = $_FILES['photo2']['name']; $photo3 = $_FILES['photo3']['name']; $car_of_the_week = $_POST['car_of_the_week']; $status1 = $_POST['status1']; $id = mysql_real_escape_string($id); $price = mysql_real_escape_string($price); $description = mysql_real_escape_string($description); $car_of_the_week = mysql_real_escape_string(car_of_the_week); $status1 = mysql_real_escape_string($status1); if($photo=""){ $sql_string = "UPDATE car SET price='$price', description='$description', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; } else { $sql_string = "UPDATE car SET price='$price', description='$description', photo='$photo', photo1='$photo1', photo2='$photo2', photo3='$photo3', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; } mysql_query($sql_string) OR die(mysql_error()); echo "<br/>SQL STRING WE WANT TO RUN: ".$sql_string; echo "db connection: ".$dbconn; echo "table connection: ".$table; $ok = mysql_query($sql_string); echo "OK: ".$ok; if($ok){ echo "<h2>all is good, upload another</h2>"; } else { echo "<h2>check the string".$sql_string."</h2>"; } } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/ Share on other sites More sharing options...
PC Nerd Posted May 20, 2007 Share Posted May 20, 2007 try : if(empty($photo)) {......} might work instead......... Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257556 Share on other sites More sharing options...
dark22horse Posted May 20, 2007 Author Share Posted May 20, 2007 It didnt work I think it may be because the file array comes up with this, so it is not empty, [photo] => Array ( [name] => [type] => [tmp_name] => [error] => 4 => 0 Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257562 Share on other sites More sharing options...
redarrow Posted May 20, 2007 Share Posted May 20, 2007 post your form the form has to correctly codded in the correct order. Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257566 Share on other sites More sharing options...
dark22horse Posted May 20, 2007 Author Share Posted May 20, 2007 K here it is <? print_r($_GET); $id=$_GET['id']; //connect to mysql //change user and password to your mySQL name and password mysql_connect("localhost","root",""); //select which database you want to edit mysql_select_db("car"); $query = "select * from car WHERE id = '$id' "; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $row= mysql_fetch_array($result, MYSQL_ASSOC); $price= $row['price']; $description= $row['description']; $photo= $row['photo']; $photo1= $row['photo1']; $photo2= $row['photo2']; $photo3= $row['photo3']; $car_of_the_week= $row['car_of_the_week']; $status1= $row['status1']; $id=$_GET['id']; echo "$photo"; $target_path = "uploads/"; $photo = $target_path . basename( $_FILES['photo']['name']); ?> <html> <body> <form enctype="multipart/form-data" action="uploader3.php" method="POST"> <table width='100%' border='1'> <tr> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <td>ID</td> <td><input type='text' name='id' value='<?php print "$id";?>'> </td> <td>Price</td> <td><input type='text' name='price' value='<?php print "$price";?>'> </td> <td>Description</td> <td><input type='text' name='description' value='<?php echo "$description";?>'> </td> <td>Photo</td> <td><input name="photo" type="file" value='<?php print "$photo";?>'> </td> <td>Photo1</td> <td><input name="photo1" type="file" /></td> <td>Photo2</td> <td><input name="photo2" type="file" /></td> <td><input name="photo3" type="file" /></td> <td>Car of the Week</td> <td><input type="text" name="car_of_the_week" length="20" value ='<?php print "$car_of_the_week";?>'></td> <td>Status</td> <td><input type="text" name="status1" length="20" value = "jjjj"/></td> <td><input type="submit" value="Upload Files" /></td> </tr> </table> </form> <? ++$i; } ?> </BODY> </html> And uploader3.php <? require_once("mysql_config.php"); $target_path = $target_path . basename( $_FILES['photo']['name']); $_FILES['photo']['tmp_name']; $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['photo']['name']); if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) { $id = $_POST['id']; $price = $_POST['price']; $description = $_POST['description']; $photo = $_FILES['photo']['name']; $photo1 = $_FILES['photo1']['name']; $photo2 = $_FILES['photo2']['name']; $photo3 = $_FILES['photo3']['name']; $car_of_the_week = $_POST['car_of_the_week']; $status1 = $_POST['status1']; $id = mysql_real_escape_string($id); $price = mysql_real_escape_string($price); $description = mysql_real_escape_string($description); $car_of_the_week = mysql_real_escape_string(car_of_the_week); $status1 = mysql_real_escape_string($status1); if($photo=""){ $sql_string = "UPDATE car SET price='$price', description='$description', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; } else { $sql_string = "UPDATE car SET price='$price', description='$description', photo='$photo', photo1='$photo1', photo2='$photo2', photo3='$photo3', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; } mysql_query($sql_string) OR die(mysql_error()); echo "<br/>SQL STRING WE WANT TO RUN: ".$sql_string; echo "db connection: ".$dbconn; echo "table connection: ".$table; $ok = mysql_query($sql_string); echo "OK: ".$ok; if($ok){ echo "<h2>all is good, upload another</h2>"; } else { echo "<h2>check the string".$sql_string."</h2>"; } } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257568 Share on other sites More sharing options...
redarrow Posted May 20, 2007 Share Posted May 20, 2007 <form enctype="multipart/form-data" action="uploader3.php" method="POST"> <table width='100%' border='1'> <tr> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <td>ID</td> <td><input type='text' name='id' value='<?php echo $id;?>'> </td> <td>Price</td> <td><input type='text' name='price' value='<?php echo $price;?>'> </td> <td>Description</td> <td><input type='text' name='description' value='<?php echo $description;?>'> </td> <td>Photo</td> <td><input name="photo" type="file" value='<?php echo $photo;?>'> </td> <td>Photo1</td> <td><input name="photo1" type="file" /></td> <td>Photo2</td> <td><input name="photo2" type="file" /></td> <td><input name="photo3" type="file" /></td> <td>Car of the Week</td> <td><input type="text" name="car_of_the_week" length="20" value ='<?php print "$car_of_the_week";?>'></td> <td>Status</td> <td><input type="text" name="status1" length="20" value = "jjjj"/></td> <td><input type="submit" value="Upload Files" /></td> </tr> </table> </form> Try this way ok, and post the varables properly ok. Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257571 Share on other sites More sharing options...
dark22horse Posted May 20, 2007 Author Share Posted May 20, 2007 no didnt work. I have no problem updating the information if the user wants to update the photos, but If they dont then it doesnt work. I want them to just be able to upload the text lines, if they dont need to update the photos. Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257582 Share on other sites More sharing options...
dark22horse Posted May 20, 2007 Author Share Posted May 20, 2007 Cheers for your help guys. I got it working sort of! I have put in a if statement, where it detects if a file has been uploaded. If it has then I run the script to input the file. else it doesnt update the pictures. My only problem now, is that i need to do it for multiple photos fields, as I have 4. Anyone suggest a way, without writing hundreds of if statements? <? require_once("mysql_config.php"); $target_path = $target_path . basename( $_FILES['photo']['name']); $_FILES['photo']['tmp_name']; $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['photo']['name']); if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) { $id = $_POST['id']; $price = $_POST['price']; $description = $_POST['description']; $photo = $_FILES['photo']['name']; $photo1 = $_FILES['photo1']['name']; $photo2 = $_FILES['photo2']['name']; $photo3 = $_FILES['photo3']['name']; $car_of_the_week = $_POST['car_of_the_week']; $status1 = $_POST['status1']; $id = mysql_real_escape_string($id); $price = mysql_real_escape_string($price); $description = mysql_real_escape_string($description); $car_of_the_week = mysql_real_escape_string(car_of_the_week); $status1 = mysql_real_escape_string($status1); $sql_string = "UPDATE car SET price='$price', description='$description', photo='$photo', photo1='$photo1', photo2='$photo2', photo3='$photo3', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; mysql_query($sql_string) OR die(mysql_error()); echo "<br/>SQL STRING WE WANT TO RUN: ".$sql_string; echo "db connection: ".$dbconn; echo "table connection: ".$table; $ok = mysql_query($sql_string); echo "OK: ".$ok; if($ok){ echo "<h2>all is good, upload another</h2>"; } else { echo "<h2>check the string".$sql_string."</h2>"; } } else{ $sql_string = "UPDATE car SET price='$price', description='$description', car_of_the_week='$car_of_the_week', status1='$status1'"." WHERE id='$id'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52219-if-help/#findComment-257802 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.