Jump to content

help with a form please


sandbudd

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/216852-help-with-a-form-please/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.