簡體   English   中英

從登錄SQL表中獲取用戶ID

[英]fetching user id from login SQL table

我最近將代碼從mysql切換到mysqli OOP,以獲得更好的實踐。 我試圖從我的mysql表中獲取user_id並將其保存在$_SESSION['user_id'] 我已經嘗試了許多不同的代碼迭代,並花了幾個小時但沒有運氣。 到目前為止, $_SESSION['user_id']返回任何內容。 它可以與我的舊mysql代碼一起使用,因此我知道它與表無關。 我還是mysqli的新手,在此方面的任何幫助將不勝感激。

這是PHP:

<?php

session_start();
$con = new mysqli("localhost","root","pw","db"); 

if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if(isset($_POST['submit'])){
  $query = "SELECT * FROM tapp_login WHERE username = ? AND password = ? LIMIT 1";
  $username = $_POST['user_name'];
  $password = $_POST['pwd'];

  $stmt = $con->prepare($query);
  $stmt->bind_param('ss', $username, $password);
  $stmt->execute();
  $stmt->bind_result($username, $password);
  $stmt->store_result();
  if($stmt->num_rows == 1){ //To check if the row exists
    while($row = $stmt->fetch()){ //fetching the contents of the row
      $_SESSION['user_id'] = $row[1];
      $_SESSION['LOGIN_STATUS']=true;
      $_SESSION['user_name'] = $username;            
      echo 'true';
    }
  }
  else {
    echo 'false';
  }
  $stmt->free_result();
  $stmt->close();
}
else{
}
$con->close();

?>

驗證有效,並且用戶名已成功存儲在會話中。 我認為錯誤在$row = $stmt->fetch()但我無法弄清楚。 提前致謝!

我認為問題是此行:

$ _SESSION ['user_id'] = $ row [1];

我認為應該是$ row [0]或$ row ['user_id']。

(注意:我使用了'user_id',但這將是保存ID的列的名稱)

從中獲取特定數據<div id="text_translate"><p>看法:</p><pre> <td> <div class="template-demo"> <button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'" class="btn btn-outline-info btn-icon-text"> <i class="ti-search btn-icon-append"></i>View </button> </td></pre><p> 路線:</p><pre> `Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`</pre><p> Controller:</p><pre> public function View_PL_Accnt(Request $request){ $id = $request->id; $data = User::find($id); return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id'])); }</pre><p> 查看_PL_Accnt.blade.php:</p><pre> <h3 class="SupAd_name">{{ data['name'] }}</h3></pre><p> 錯誤:</p><p> 使用未定義的常量數據 - 假定為“數據”(這將在未來版本的 PHP 中引發錯誤)(視圖:C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index \View_PL_Accnt.blade.php)</p><p> <a href="https://i.stack.imgur.com/kbAZb.png" rel="nofollow noreferrer">錯誤</a></p></div>(使用 Id)在 Laravel<table> </table>

[英]Fetching particular data from <table> (using Id) in Laravel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 根據用戶 ID 從 db 表中獲取值 使用mysqli和PHP從表中獲取用戶ID 將user_id從sql中的一個表轉換為另一個表 使用 PHP 和 jQuery 從 MySQL 獲取數據並顯示在與用戶 ID 匹配的 HTML 表中 WordPress登錄名(用戶標識)和特定表數據 從登錄會話中獲取ID並填充到主頁中:PHP和mysql 使用SQL從表中獲取行時出錯 需要幫助,使用php從mysql表中獲取ID 從中獲取特定數據<div id="text_translate"><p>看法:</p><pre> <td> <div class="template-demo"> <button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'" class="btn btn-outline-info btn-icon-text"> <i class="ti-search btn-icon-append"></i>View </button> </td></pre><p> 路線:</p><pre> `Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`</pre><p> Controller:</p><pre> public function View_PL_Accnt(Request $request){ $id = $request->id; $data = User::find($id); return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id'])); }</pre><p> 查看_PL_Accnt.blade.php:</p><pre> <h3 class="SupAd_name">{{ data['name'] }}</h3></pre><p> 錯誤:</p><p> 使用未定義的常量數據 - 假定為“數據”(這將在未來版本的 PHP 中引發錯誤)(視圖:C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index \View_PL_Accnt.blade.php)</p><p> <a href="https://i.stack.imgur.com/kbAZb.png" rel="nofollow noreferrer">錯誤</a></p></div>(使用 Id)在 Laravel<table> </table> 從SQL表中獲取文本值以在標頭中使用:位置
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM