Jump to content

Updating uploaded picture in MySQL using PHP


kunyomi

Recommended Posts

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><br>
      Email: <INPUT TYPE="text" NAME="email" VALUE="<?php echo $myrow["email"]; ?>" SIZE=30><br>
      Who: <INPUT TYPE="text" NAME="age" VALUE="<?php echo $myrow["age"]; ?>" SIZE=30><br>
      Birthday: <INPUT TYPE="text" NAME="birthday" VALUE="<?php echo $myrow["birthday"]; ?>" SIZE=30><br>
      Address: <TEXTAREA NAME="address" ROWS=10 COLS=30><?php echo $myrow["address"]; ?></TEXTAREA><br>
      Number: <INPUT TYPE="text" NAME="number" VALUE="<?php echo $myrow["number"]; ?>" SIZE=30><br>
      Contact Image: <INPUT NAME="uploadedfile" VALUE="<?php echo $myrow["uploadedfile"]; ?>" TYPE="file" /><br>

   
      <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><br><br>";
  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 "<br>";
//Tells you if its all ok
echo "<br>";

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>";
}
}

?>

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.