Php 在mysql中更新上载文件夹中的图像和图像路径
我正在尝试更新我的上传文件夹和mysql数据库中的图像。上传的文件名为0.jpg,而不是普通人id 13.jpg,并且不会在mysql数据库中更新。下面是我的代码片段,我做错了什么Php 在mysql中更新上载文件夹中的图像和图像路径,php,mysql,Php,Mysql,我正在尝试更新我的上传文件夹和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']
$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.";
}
?>
而不是这个
$staff\u id=mysql\u insert\u id()代码>
$new\u file\u name=mysql\u insert\u id()$分机代码>
//我已删除,photo='$target'只显示id作为图片名
mysql_查询(“更新开发集照片='$new_file_name',其中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
首先,您使用的是mysql_*函数,从5.5开始就不推荐使用
其次,您需要查看手册页面以了解更多信息。引述:
检索由
上一个查询(通常是插入)
这意味着您只能在将数据插入或更新users/persons表后调用mysql_insert_id()。然而,在您的情况下,似乎您已经在变量$staff\u ID
中存储了该人员的ID,因此您甚至可能不需要使用mysql\u insert\u ID。这样做会不会不起作用
$target = "images/" . $staff_id . $ext;
您正在调用mysql\u insert\u id()
,但从未执行过插入操作,因此会返回一个布尔值false/0。请小心。开发可能不是用户/人员表。如果不是这样,您将获得开发的主键,而不是用户。而且可能整个体系结构都是错误的。我想你需要一张上传文件表和一张人员表。每个用户可以上传很多图片。因此,您首先需要将一个新图像与上载该图像的用户的id一起插入到图像表中(要获取该用户的id,您可能需要另一个查询,或者您在会话中拥有该id)。然后,您获取刚刚上传的图像的id,并对其执行任何您需要的操作,例如,如果您有类似“last submitted image”字段的内容,则将其存储在用户表中。当我在更新之前插入它时,它会在数据库中创建一条新记录,而不是更新否,它不会。请参阅文档:检索上一个查询(通常是插入)为自动增量列生成的ID。我尝试了您的解决方案,但它给了我一个提示:未定义变量:staff\u ID