[英]php sql upload picture to logged in user
Hello I want to upload a pic to phpmyadmin and to store it to the loggen in user but can't manage it.您好,我想将图片上传到 phpmyadmin 并将其存储到用户中的 loggen 但无法管理它。 And when I succed on uploading the image it goes to the first user.Want to return some rows of the table, but of the logged in user!当我成功上传图像时,它会转到第一个用户。想要返回表的某些行,但返回登录用户的行! Here is the code!这是代码! Thanks谢谢
include('functions.php');
$upload_dir = 'images/';
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
echo $file_name;
echo '<br>';
echo $file_size;
echo '<br>';
echo $file_tmp;
echo '<br>';
echo $file_type;
echo '<br>';
echo $file_ext;
echo '<br>';
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="Μόνο JPEG ή PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
$path = $upload_dir.$file_name;
echo $file_tmp.' >> '.$path;
echo '<br>';
//check the folder status _START
if (!is_dir($upload_dir) || !is_writable($upload_dir)) {
echo 'Upload directory('.$upload_dir.') is not writable, or does not exist.';
}
//_END
if(move_uploaded_file($file_tmp, $upload_dir.$file_name)===TRUE) {
$logged_in_user_id = mysql_insert_id($db);
$sql ="UPDATE users SET profile_image='$path' WHERE id='$id'";
echo $sql;
echo '<br>';
$result = mysqli_query($db, $sql);
echo "Success";
}
}else{
print_r($errors);
}
}
Problem is in how you get the user ID for your insert:问题在于如何获取插入的用户 ID:
$logged_in_user_id = mysql_insert_id($db)
mysql_insert_id($db)
will get the ID of your last insert that generates an autoincrement. mysql_insert_id($db)
将获取生成自动增量的最后一次插入的 ID。 I do not believe it will work as intended if you have closed the connection after your insert.如果您在插入后关闭连接,我认为它不会按预期工作。 If your insert is on a different script, use sessions or $_GET
or $_POST
to transfer your user ID.如果您的插入是在不同的脚本上,请使用会话或$_GET
或$_POST
来传输您的用户 ID。
Also the reason could be incompatibility between packages:原因也可能是包之间的不兼容:
you are using mysqli
to write to db.您正在使用mysqli
写入数据库。 but retrieving the last ID with mysql
package.但是使用mysql
包检索最后一个 ID。 mysqli
also has a method for this operation: mysqli_insert_id()
mysqli
也有这个操作的方法: mysqli_insert_id()
Also you get your user ID in to variable $logged_in_user_id
but in your query you are using variable $id
...此外,您将用户 ID 放入变量$logged_in_user_id
但在您的查询中,您使用的是变量$id
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.