为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
阿晨1998 2023-04-15 20:52:28
我如何使用 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');      }    }
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

我会假设你已经完成了登录认证。


您必须在注册页面中包含类型文件的输入。


这是插入照片所需的 PHP 代码:


  extract($_POST);


  if(isset($upload)) // Upload variable here is the button user clicks when he registers

    {



        $query = "insert into users (picture)values (?)";

            $result = $db->prepare($query);



            $target_path = "profilePictures/" . $un . "_";

            $target_path = $target_path.basename($_FILES['profilePic']['name']); 


            if (move_uploaded_file($_FILES['profilePic']['tmp_name'], $target_path))

            {

                $newPic = "profilePictures/" . $un . "_" . basename($_FILES['profilePic']['name']);

                $result->bindParam(1, $newPic);


            }


            $result->execute();

            $db=NULL;

            if ($result)

            {

                $success = true;



            success("Information inserted Successfully "); //success here is a function i created you can just echo this message instead.

            }

            else

            {

                error("Failed");

            }

        }

    }

现在,当用户从登录页面登录时,您将不得不在需要的地方进行一些编码:


 session_start();


 $username=$_SESSION['activeUser']; // 

  query="select * from users where username='$username'";

  $result = $db->prepare($query);

$result->execute();

$row=$result->fetch();

$userpic=$row['picture'];

 $_SESSION['picture'] = $userpic; // here the image will be saved in a session and you 

 can save it in a variable elsewhere and use it.



查看完整回答
反对 回复 2023-04-15
  • 1 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信