0

我正在尝试更新上传文件夹和 mysql 数据库中的图像文件上传文件名 0.jpg 而不是普通人 id 13.jpg 并且不会在 mysql 数据库中更新,这是我在下面的片段我做错了什么?

$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.";
}
?>
4

2 回答 2

0

而不是这个

$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");

做这样的事情

mysql_query ("INSERT INTO development (photo) VALUES ( '".$new_file_name."' )");    
//first insert  
$staff_id = mysql_insert_id() ;     
// then get the id of the record you've just inserted
于 2013-09-27T18:23:43.337 回答
0

首先,您使用的是 mysql_* 函数,从 5.5 开始不推荐使用。

其次,您需要查看mysql_insert_id的手册页。引用:

检索上一个查询(通常是 INSERT)为 AUTO_INCREMENT 列生成的 ID。

这意味着您只能在将数据插入或更新用户/人员表之后调用 mysql_insert_id()。但是,在您的情况下,似乎您已经将人员的 ID 存储在变量$staff_id中,因此您可能甚至不需要使用 mysql_insert_id。这不可行吗?

$target = "images/" . $staff_id . $ext;
于 2013-09-27T18:24:08.700 回答