使用PHP在mysql数据库中上传图像

时间:2017-06-03 17:56:38

标签: php mysql

我有一个有acoounts的网站。我希望每个用户上传一个头像。我尝试运行代码时出现错误: 解析错误:语法错误,意外'' (T_ENCAPSED_AND_WHITESPACE),期望第26行/var/www/html/upload_image.php中的标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

如果有人能告诉我我错在哪里,我会贬低这一点。谢谢。

<?php
   session_start();

   $connect = mysqli_connect("localhost", "stringdot", "Ninja123") or die( mysqli_error()) ; 
   $db = mysqli_select_db($connect, "ddbase") or die( mysqli_error($connect));

   if(isset($_FILES['image'])){
      $error= array();
      $marime_fisier =$_FILES['image']['size'];
      $extensia= $_FILES['image']['type'];
      $f = explode('.',$_FILES['image']['name']);
      $file_extension=strtolower(end($f));

      $extensions= array("jpeg","jpg","png");

      if(in_array($file_extension,$extensions)=== false){
         $error[]="Please upload a file with extension JGEG or PNG.";
      }

      if($marime_fisier > 2097152){
         $error[]='File size cannot exceed 2 MB';
      }

      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
      if(empty($error)==true){
         $sql = " UPDATE users SET avatar = '{$image}' WHERE username = $_SESSION['login_username'] ";
         $result = mysqli_query($connect,$sql) or die ( "Error : ". mysqli_error($connect) );
         $rows = mysqli_num_rows($result);
         if ($rows==1) {
            echo "Success";}
            else {
                 echo "Something went wrong!";;
            }
      }else
         {
         echo $error;
      }
   }
?>

HTML code:

<form class = "file_upload_form" action="upload_image.php" method="POST" enctype="multipart/form-data">
    <label>File: </label><input type="file" name="image" /></br>
    <input type="submit" />
</form>

1 个答案:

答案 0 :(得分:0)

只需用你的

替换你的php代码的第26行
$sql = " UPDATE users SET avatar = '$image' WHERE username =".$_SESSION['login_username'] ;

我相信它会奏效。