使用 Php 会话从数据库中获取文件

我如何使用 sql 将图像作为注册数据的一部分上传到数据库,并在每次用户登录时通过 php 会话获取它我已经有了这个用于保存用户名、电子邮件和密码..但我想添加图像作为个人资料图片注册并且应该能够在用户每次登录时调用它


    <?php

    session_start();


    $username = "";

    $email    = "";

    $errors = array(); 


    $db = mysqli_connect('localhost', 'root', '', 'registration');


    if (isset($_POST['reg_user'])) {

      $fname = mysqli_real_escape_string($db, $POST['fname']);

      $username = mysqli_real_escape_string($db, $_POST['username']);

      $email = mysqli_real_escape_string($db, $_POST['email']);

      $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);

      $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);


      if (empty($fname)) { array_push($errors, "Please Enter your full name");}

      if (empty($username)) { array_push($errors, "Username is required"); }

      if (empty($email)) { array_push($errors, "Email is required"); }

      if (empty($password_1)) { array_push($errors, "Password is required"); }

      if ($password_1 != $password_2) {

        array_push($errors, "The two passwords do not match");

      }


      $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";

      $result = mysqli_query($db, $user_check_query);

      $user = mysqli_fetch_assoc($result);


      if ($user) { 

        if ($user['username'] === $username) {

          array_push($errors, "Username already exists");

        }


        if ($user['email'] === $email) {

          array_push($errors, "email already exists");

        }

      }



      if (count($errors) == 0) {

        $password = md5($password_1);


        $query = "INSERT INTO users (fname, username, email, password) 

                  VALUES( '$fname', '$username', '$email', '$password')";

        mysqli_query($db, $query);

        $_SESSION['username'] = $username;

        $_SESSION['success'] = "You are now logged in";

        header('location: /me/home.php');

      }

    }



阿晨1998
浏览 126回答 1
1回答

弑天下

我会假设你已经完成了登录认证。您必须在注册页面中包含类型文件的输入。这是插入照片所需的 PHP 代码:&nbsp; extract($_POST);&nbsp; if(isset($upload)) // Upload variable here is the button user clicks when he registers&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $query = "insert into users (picture)values (?)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $db->prepare($query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $target_path = "profilePictures/" . $un . "_";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $target_path = $target_path.basename($_FILES['profilePic']['name']);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (move_uploaded_file($_FILES['profilePic']['tmp_name'], $target_path))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newPic = "profilePictures/" . $un . "_" . basename($_FILES['profilePic']['name']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result->bindParam(1, $newPic);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result->execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $db=NULL;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $success = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success("Information inserted Successfully "); //success here is a function i created you can just echo this message instead.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error("Failed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }现在,当用户从登录页面登录时,您将不得不在需要的地方进行一些编码:&nbsp;session_start();&nbsp;$username=$_SESSION['activeUser']; //&nbsp;&nbsp; query="select * from users where username='$username'";&nbsp; $result = $db->prepare($query);$result->execute();$row=$result->fetch();$userpic=$row['picture'];&nbsp;$_SESSION['picture'] = $userpic; // here the image will be saved in a session and you&nbsp;&nbsp;can save it in a variable elsewhere and use it.
打开App,查看更多内容
随时随地看视频慕课网APP