繁体   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