954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Editing uploaded picture in MySQL using PHP

Hi guys, I'm trying to UPDATE the image that has been already in the database with another image using a form in PHP.

I got the uploading working all fine already. Just the edit part.

With this code, I get no error, but the MySQL table is not written with the new image name and the image does not get uploaded into the 'uploads' folder.

<?php
include ("dbConfig.php");
require ("check.php");

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id2 = $_GET["id2"];
      $sql = "SELECT * FROM contacts WHERE id2=$id2";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
	  
      <form action="<?php $v=explode('?',$_SERVER['PHP_SELF']); echo $v[0]; ?>" method="post">
      <input type=hidden name="id2" value="<?php echo $myrow["id2"]; ?>">
   
      Name: <INPUT TYPE="text" NAME="name" VALUE="<?php echo $myrow["name"]; ?>" SIZE=30>
      Email: <INPUT TYPE="text" NAME="email" VALUE="<?php echo $myrow["email"]; ?>" SIZE=30>
      Who: <INPUT TYPE="text" NAME="age" VALUE="<?php echo $myrow["age"]; ?>" SIZE=30>
      Birthday: <INPUT TYPE="text" NAME="birthday" VALUE="<?php echo $myrow["birthday"]; ?>" SIZE=30>
      Address: <TEXTAREA NAME="address" ROWS=10 COLS=30><?php echo $myrow["address"]; ?></TEXTAREA>
      Number: <INPUT TYPE="text" NAME="number" VALUE="<?php echo $myrow["number"]; ?>" SIZE=30>
      Contact Image: <INPUT NAME="uploadedfile" VALUE="<?php echo $myrow["uploadedfile"]; ?>" TYPE="file" />

   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
      
<?php      
   }
}
$target = "C:/Program Files/xampp/htdocs/cas/uploads/";
$target = $target . basename( $_FILES['uploadedfile']['name']);

   if (isset($_POST['submit'])) {
   	  $id2 = mysql_real_escape_string(stripslashes($_POST["id2"]));
      $name = mysql_real_escape_string(stripslashes($_POST["name"]));
	  $email = mysql_real_escape_string(stripslashes($_POST["email"]));
	  $age = mysql_real_escape_string(stripslashes($_POST["age"]));
	  $birthday = mysql_real_escape_string(stripslashes($_POST["birthday"]));
	  $address = mysql_real_escape_string(stripslashes($_POST["address"]));
	  $number = mysql_real_escape_string(stripslashes($_POST["number"]));
	  $uploadedfile=($_FILES['uploadedfile']['name']);
 
	  $sql = "UPDATE contacts SET name='$name', email='$email', age='$age', birthday='$birthday', address='$address', number='$number', uploadedfile='$uploadedfile' WHERE id2=$id2";
 
      $result = mysql_query($sql) or die(mysql_error());
      echo "<div align=\"center\"><b>Thank you! Information updated.</b>";
	  echo "<a href=\"http://localhost/cas/contacts.php\">Contacts</a>"; echo " | ";
	  echo "<a href=\"http://localhost/cas/members.php\">Member Home</a>";echo " | ";
	  echo "<a href=\"http://localhost/cas/logout.php\">Logout</a></div>";echo "  ";
	  
	  //check image upload status
	  if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
{
echo "Thank you, " . $_SESSION["valid_user"];echo ". Not you? <a href=\"http://localhost/cas/logout.php\">Click here.</a>";
echo "";
//Tells you if its all ok
echo "";

echo "The file <b>". basename( $_FILES['uploadedfile']['name']). "</b> has been uploaded.";
echo "</div>";

}
else {

//Gives and error if its not
echo "<div align=\"center\">";
echo "Sorry, there was a problem uploading your file.";
echo "</div>";
}
	}
	
?>
kunyomi
Newbie Poster
14 posts since Apr 2010
Reputation Points: 15
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: