亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號