sandbudd Posted October 26, 2010 Share Posted October 26, 2010 Here is what I have, I have a script to update an image in the db and a script to put the image in a folder. If I use this <form id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> it will populate the database and not upload the image. If I use this <form id="form2" name="form2" method="post" action="?id=<? echo $id; ?>&page=update" enctype="multipart/form-data"> it will upload the image and not populate the database. Help I'm stuck..lol! <?php // Connect database. // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if($_GET['page'] == "update") { $id=$_GET['id']; $photo=$_POST['photo']; // Do update statement. mysql_query("update ads set photo='$photo' where id='$id'") or die(mysql_error()); } // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) $id=$_GET['id']; // Get records in all columns from table where column id equal in $id and put it in $result. $result=mysql_query("select * from ads where id='$id'"); // Split records in $result by table rows and put them in $row. $row=mysql_fetch_assoc($result); ?> <?php // Close database connection. mysql_close(); ?> <?php //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $pic=($_FILES['photo']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <body> <br> <br> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> <table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Photo</span></p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong><?php echo "<img src=images/".$row['photo'] ."> "; ?><br> </strong></span><strong> <input type="file" name="photo" /> </strong></p></td> </tr> </table> <p> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/ Share on other sites More sharing options...
sandbudd Posted October 26, 2010 Author Share Posted October 26, 2010 If anyone can give me a better way to accomplish what I am doing would be great! Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126517 Share on other sites More sharing options...
wigpip Posted October 26, 2010 Share Posted October 26, 2010 noticed in your first form "form1" you haven't got the enctype="multipart/form-data" Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126521 Share on other sites More sharing options...
sandbudd Posted October 26, 2010 Author Share Posted October 26, 2010 Hi wigpip it has to be or won't upload the image...with it there it wont populate the database? Without out it will populate the database but wont upload the image? Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126524 Share on other sites More sharing options...
wigpip Posted October 26, 2010 Share Posted October 26, 2010 Hi, so you've tried? and it won't work? <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126528 Share on other sites More sharing options...
sandbudd Posted October 26, 2010 Author Share Posted October 26, 2010 yes wigpip I have Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126530 Share on other sites More sharing options...
wigpip Posted October 26, 2010 Share Posted October 26, 2010 i thought the action command looked empty notice in the commented out hint PHP SELF use {$_SERVER['PHP_SELF']} before the "?" in the action of the form url Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126532 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 Use the enctype="multipart/from-data"> and the line below needs to be changed. The filename will be in the $_FILES array, not the $_POST array. if($_GET['page'] == "update") { $id=$_GET['id']; $photo=$_FILES['photo']['name']; // < --- this line That's as far into it as I got, but try that and see if it takes care of the issue. Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126533 Share on other sites More sharing options...
sandbudd Posted October 26, 2010 Author Share Posted October 26, 2010 Perfect Pikachu2000 works great and I will never forget it...lol...thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126536 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 You're welcome. Please remember to mark the thread solved (if indeed it is solved) using the button on the lower left side of the page . . . Nevermind, you beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/#findComment-1126537 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.