dongqiongzheng0615
2016-09-29 19:52
浏览 114
已采纳

如何获取数据库中特定行的相应user_id(自动增量)值?

My table has 2 columns, user_id and username. The user logs in with username like this :

<?php

require('dbConnect.php');

$username = $_POST['username'];

//need to keep this in a session, for other pages later on
session_start();
    $_SESSION['username'] = $username;

$sql = "SELECT * FROM user WHERE username = '$username'";
$result = mysqli_query($con,$sql);

$check = mysqli_fetch_array($result);

if(isset($check)) :

//if the username exists in the database, then show a html submit button
$con->close();
?>
     <html>
<body>
<form action="UserDetails.php" method="post">
 <input type="submit">
</form>
     </html>

<?php  else :{
    //if user is not in db, show this message
         echo 'Sorry about that, you can't come in.';
     }
     $con->close();
 ?>
 <?php endif; ?>

How can I get the user_id that corresponds to username, to be used in pages later on ?

图片转代码服务由CSDN问答提供 功能建议

我的表有2列,user_id和username。 用户使用以下用户名登录:

 &lt;?php 
 
require('dbConnect.php'); 
 
 $ username = $ _POST [' 用户名']; 
 
 //需要将其保留在会话中,其他页面稍后会在
session_start(); 
 $ _SESSION ['username'] = $ username; 
 
 $ sql =“SELECT  * FROM user WHERE username ='$ username'“; 
 $ result = mysqli_query($ con,$ sql); 
 
 $ check = mysqli_fetch_array($ result); 
 
if(isset($ check))  :
 
 //如果用户名存在于数据库中,则显示一个html提交按钮
 $ con&gt; close(); 
?&gt; 
&lt; html&gt; 
&lt; body&gt; 
&lt;  ; form action =“UserDetails.php”method =“post”&gt; 
&lt; input type =“submit”&gt; 
&lt; / form&gt; 
&lt; / html&gt; 
 
&lt;?php else:  {
 //如果用户不在数据库中,请显示此消息
 echo'对不起,您无法进入。'; 
} 
 $ con&gt; close(); 
?&gt;  ; 
&lt;?php endif;  ?&gt; 
   
 
 

如何获取与用户名相对应的user_id,以便稍后在页面中使用?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doulu4976 2016-09-30 23:45
    已采纳

    The (isset($check)) seemed to be causing me trouble, sometimes returning user_id, sometimes not.

    Thanks in large part to Nabeel's answer, this idea worked for me. And also thanks to Solrac Ragnarockradio for putting me on track to make my code more secure with mysqli_real_escape_string :

    <?php
    require('dbConnect.php');
    
    $username = mysqli_real_escape_string($con,$_POST['username']);
    
    $sql = "SELECT * FROM user WHERE username = '$username'";
    $result = mysqli_query($con,$sql);
    
    $row = mysqli_fetch_assoc($result);
    $user_id = $row["user_id"];
    
    //give me the corresponding user_id of the logged in user
    echo $user_id;
    
    if (mysqli_num_rows($result)==0) {
        echo "Failed, sorry";
    }
    
    if (mysqli_num_rows($result) > 0) {
        echo "User id exists already.";
    
        }
    
    $con->close();
    ?>
    
    打赏 评论
  • duanbeng8872 2016-09-29 19:56

    After these lines:

    $sql = "SELECT * FROM user WHERE username = '$username'";
    $result = mysqli_query($con,$sql);    
    

    Add these lines:

    $row = mysqli_fetch_assoc($result);
    $user_id = $row["user_id"];
    
    打赏 评论
  • dongsun5330 2016-09-29 19:59

    isset($check) isnt save at this point, better check for the value of $check and the {} after else arent needed and it will not work.

    打赏 评论
  • droxlzcgnr639823 2016-10-01 00:37

    A few problems here:

    1) If you are using "SELECT * FROM user WHERE username = $1 then username column should be UNIQUE and never forget to escape your input:

    <?php
    $username = mysqli_real_escape_string($con, $_POST['username']);
    $sql_query = mysqli_query($con,"SELECT * FROM user WHERE username = '$username'");
    

    2) You could check if you have any results b4 anything and assign those vars straight from the db...

    if (mysqli_num_rows($sql) == 0) {
     $error = "This user doesn't exists here...";
    }else{
      $user_info = mysqli_fetch_assoc($sql_query);
      $_SESSION['username'] = $user_info['username'];
      $_SESSION['user_id'] = $user_info['user_id'];
    
    }
    
    $con->close();
    ?>
    

    Then you can output whatever you want for logged in users if $_SESSION['user_id'] is actually there...

    <html>
     <body>
      <form action="UserDetails.php" method="post">
      <input type="submit">
      </form>
    </body>
    
    <?php
     echo 'Well, here\'s the output: <b>'. (isset($_SESSION['user_id']) ? $_SESSION['username'] : $error).'</b>';
    
    打赏 评论

相关推荐 更多相似问题