简体   繁体   English

从登录SQL表中获取用户ID

[英]fetching user id from login SQL table

I have recently switched my code from mysql to mysqli OOP for better practice. 我最近将代码从mysql切换到mysqli OOP,以获得更好的实践。 I am trying to fetch the user_id from my mysql table and save it in $_SESSION['user_id'] . 我试图从我的mysql表中获取user_id并将其保存在$_SESSION['user_id'] I have tried many different iterations of my code and worked on it for hours but no luck. 我已经尝试了许多不同的代码迭代,并花了几个小时但没有运气。 As of now $_SESSION['user_id'] returns nothing. 到目前为止, $_SESSION['user_id']返回任何内容。 It works with my old mysql code so I know its not an issue with my table. 它可以与我的旧mysql代码一起使用,因此我知道它与表无关。 I am still new at mysqli and I would greatly appreciate any help in this matter. 我还是mysqli的新手,在此方面的任何帮助将不胜感激。

Here is the PHP: 这是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();

?>

The validation works and the user name is successfully stored in the session. 验证有效,并且用户名已成功存储在会话中。 I think the error is in the $row = $stmt->fetch() but I cannot figure it out. 我认为错误在$row = $stmt->fetch()但我无法弄清楚。 Thanks in advance! 提前致谢!

I think the issue is this line: 我认为问题是此行:

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

I believe it should either be $row[0] or $row['user_id']. 我认为应该是$ row [0]或$ row ['user_id']。

(note: I used 'user_id', but it would be the name of the column that holds the IDs) (注意:我使用了'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 表中获取值 - Fetching the value from db table as per user id 使用mysqli和PHP从表中获取用户ID - fetching user ID from a table using mysqli and PHP 将user_id从sql中的一个表转换为另一个表 - Translating user_id from one table in sql to another table 使用 PHP 和 jQuery 从 MySQL 获取数据并显示在与用户 ID 匹配的 HTML 表中 - Fetching Data from MySQL using PHP & jQuery and displaying in HTML table matching the user ID WordPress登录名(用户标识)和特定表数据 - WordPress Login (User ID) & Specific Table Data 从登录会话中获取ID并填充到主页中:PHP和mysql - fetching id from login session and populating in home page : php and mysql 使用SQL从表中获取行时出错 - Error in fetching rows from table using SQL 需要帮助,使用php从mysql表中获取ID - need help fetching the id from mysql table with php 从中获取特定数据<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 从SQL表中获取文本值以在标头中使用:位置 - Fetching text value from SQL table to use in header: Location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM