我正在尝试更新我的上传文件夹和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.";
}
?>
答案 0 :(得分: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
答案 1 :(得分:0)
首先,您正在使用mysql_ *函数,这些函数自5.5以来已弃用。
其次,您需要查看mysql_insert_id的手册页。引用:
检索由AUTO_INCREMENT列生成的ID 以前的查询(通常是INSERT)。
这意味着您只能在将数据插入或更新用户/人员表后调用mysql_insert_id()。但是,在您的情况下,似乎您已经拥有存储在变量$staff_id
中的人员的ID,因此您甚至可能不需要使用mysql_insert_id。这不会起作用吗?
$target = "images/" . $staff_id . $ext;