简体   繁体   English

如何在id =处插入图片名称? …在mySQL [PHP]中

[英]How to insert imagename where id=? … in mySQL [PHP]

I am trying to upload avatar images to MySQL database. 我正在尝试将头像图像上传到MySQL数据库。 My code is working correctly, but the problem is with the SQL query... 我的代码正常运行,但是问题出在SQL查询上。

This is my code: 这是我的代码:

<?php
if(isset($_POST)){
    if(isset($_FILES['avatar'])){
      $errors= array();
      $file_name = $_FILES['avatar']['name'];
      $file_size =$_FILES['avatar']['size'];
      $file_tmp =$_FILES['avatar']['tmp_name'];
      $file_type=$_FILES['avatar']['type'];

//print_r($_FILES['avatar']['name']); 

  $file_ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);


      //$file_ext=strtolower(end(explode('.',$_FILES['avatar']['name'])));

      $expensions= array("jpg","gif","jpeg","png","bmp");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPG, GIF, BMP or PNG formats...";
      }

      if($file_size > 8000000){
         $errors[]='File size must be excately 8 MB';
      }
//print_r($file_tmp); exit;
      if(empty($errors)==true){
          $randname = (rand(0,1000));
          $file_data=date("d-m-Y-H-i-s");
          $file_name=$file_data.$randname;
         move_uploaded_file($file_tmp,"avatars/".$file_name);
         echo "Success";
        $currentsessionid = $_SESSION["userid"];
        $query = $conn->prepare("UPDATE users SET
avatar = ?, WHERE id = ?");
$insert = $query->execute(array(
      "$file_name","$currentsessionid"
));
      }else{
         print_r($errors);
      }
   }
} } ?>  

Look at the SQL query carefully, I have table users and columns id , avatar ... avatar column is varchar , just i want to put filename to avatar where current logged in user id .. 仔细看一下SQL查询,我有表usersid列, avatar ... avatar列是varchar ,只想将文件名放到current logged in user id avatar中。

There is an error in your insert-statement. 您的插入语句中有错误。

An Insert have to look something like that: 插入必须看起来像这样:

INSERT INTO users (avatar, id) VALUES('avatar.png', $sessionid)

There is no session_start() in the code you've shown us, nor is there anything to initialize your database connection. 您显示给我们的代码中没有session_start() ,也没有什么可以初始化您的数据库连接。 You've also not said what the problem is . 您也没有说出问题所在

You should be checking the return values from $conn->prepare() and $query->execute() . 您应该检查$conn->prepare()$query->execute()的返回值。 If they are false, then you should be polling for errors then quoting them here if you can't solve the problem yourself. 如果错误,则应该轮询错误,如果您自己无法解决问题,请在此处引用错误。

INSERT INTO users SET avatar = ?, WHERE id = ? 插入用户SET SET avatar =?,WHERE id =?

...is not SQL. ...不是SQL。 You are confusing the syntax for INSERT with the syntax for UPDATE. 您正在将INSERT的语法与UPDATE的语法混淆。 jiGL has provided and example of how to to write an INSERT properly. jiGL提供了如何正确编写INSERT的示例。 Since you presumably already have a rescord in users representing the user, and want to change this to include the avatar path: 既然你想必已经在rescord users代表用户,并希望改变这包括头像路径:

UPDATE users SET avatar = ? WHERE id = ?

BTW - the filename you are generating (kudos for not using the name supplied by the user) doesn't have an extension which makes it rather difficult to serve up again. 顺便说一句-您生成的文件名( 不使用用户提供的名称的称呼 )没有扩展名,因此很难再次提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM