个人资料图片未更新

时间:2013-09-27 22:05:58

标签: php mysql

我需要帮助使这个脚本工作,但一切都无济于事,它没有在sql中更新,但它上传到上传目录,而不是名称为0.jpg,而是附加到像7.jpg这样的beging的staff_id,我需要更正或重新编写脚本

<?php 
   $allowed_filetypes = array('.jpg','.pdf','.xlsx','.xls','.doc','.docx','.ppt','.pptx','.jpeg','.png','.gif','.pdf');
   $max_filesize = 52428800; // max file size = 50MB
   $target = "images/"; 
   $pic=($_FILES['photo']['name']);
$pic = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));



   //this gets all the other information from the form

 $pic=($_FILES['photo']['name']);

    $file = $_FILES['photo']['name']; // Get the name of the file (including file extension).
    $ext = substr($file, strpos($file,'.'), strlen($file)-1);
    if(!in_array($ext,$allowed_filetypes))//check if file type is allowed
        die('The file extension you attempted to upload is not allowed.'); //not allowed
    if(filesize($_FILES['photo']['tmp_name']) > $max_filesize) //check that filesize is less than 50MB
        die ('The file you attempted to upload is too large, compress it below 50MB.');


   // Connects to your Database
     mysql_connect("localhost", "root", "") or die(mysql_error()) ;
     mysql_select_db("office") or die(mysql_error()) ;

    //writes the information to the 



  $target = "images/" .mysql_insert_id() . $ext; 

  $staff_id = mysql_insert_id();
  $new_file_name = mysql_insert_id() . $ext;


  //I removed ,photo='$target' to display only id as picture name
  mysql_query("UPDATE development SET photo='$new_file_name' WHERE staff_id=$staff_id");


//writes the file to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

 //tells you if its all ok
  echo "The file ". basename( $_FILES['photo']['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.";
}
?>

1 个答案:

答案 0 :(得分:2)

对于初学者,您甚至在运行查询之前尝试访问ID:

 $target = "images/" .mysql_insert_id() . $ext;

 $staff_id = mysql_insert_id();
 $new_file_name = mysql_insert_id() . $ext;

那不行。您需要先运行查询