我正在使用登录创建一个小帐户,现在我必须显示登录的ID
名称。假设我现在有两个帐户进入我的数据库,因为我打印他的名字,它显示ID no 。我使用ID号登录时的名字。你能告诉我发生了什么吗?在哪里我认为是错的?,
这是我的change_setting_db.php
:
<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin");
?>
这是我的general_setting.php
:
<div id="change_name">
<label><strong>Name: </strong></label>
<?php
include('change_setting_db.php');
while($row = mysqli_fetch_array($result))
{
echo $row['first_name']." ".$row['last_name'];
}
?>
<a id="display_float" href="change_name.php">Edit</a>
</div><hr>
EDITED
我试过但它不起作用..
<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin");
if ($row = mysqli_fetch_array($result)) {
$id=$row['id'];
mysqli_query($con,"SELECT * FROM admin WHERE id='$id' ");
}
?>
答案 0 :(得分:1)
当您在$result = mysqli_query($con,"SELECT * FROM admin");
中对数据库进行查询时,您需要传递存储在会话变量中的用户ID。
看看这个:
$uid = $_SESSION['uid'];
$result = mysqli_query($con, "SELECT * FROM admin WHERE uid = '$uid'");
答案 1 :(得分:0)
你的SQL查询应该是
SELECT * FROM admin WHERE id='1';
答案 2 :(得分:0)
为什么您希望此代码显示登录用户,因为您正在执行:
SELECT * FROM admin
这将检索表中没有条件的所有行。您需要添加WHERE
子句,例如:
SELECT * FROM admin WHERE user_id = ?
通常,登录用户的user_id
将存储在会话中。还要记住使用预准备语句,而不是将user_id
直接连接到查询中。
答案 3 :(得分:0)
这里传递&#34; ID&#34;用户在查询条件的位置,如果您想要特定用户名。
$con=mysqli_connect("localhost","root","Bhawanku","members");
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin where id=(pass your id's value here)");
?>
或
当你手中有id时,只需点击
之类的条件选择查询$ result = mysqli_query($ con,&#34; select * from admin其中id =&#39;。&#34; $ id&#34;。&#39;&#34;); 如果(计数($结果大于0) { //现在显示该用户的名称 echo&#34; Id =&#34;。$ result [0] [&#39; id&#39;]; echo&#34; Name =&#34;。$ result [0] [&#39; first_name&#39;]。&#34; &#34; $结果[0] [&#39;如first_name&#39;]; } 其他 { //在这里处理错误情况 echo&#34;未找到结果&#34 ;; }
答案 4 :(得分:0)
SELECT * FROM admin WHERE username = $username AND password = $password);
Row ID:<?= $row['id'] ?>
答案 5 :(得分:0)
我认为这应该会帮助你,
<!DOCTYPE html>
<html>
<head>
</head>
<body id=""body>
<div id="body-container">
<form action="index.php" method="POST">
<input type="text" id="username" name="username" value="" autocomplete="false" spellcheck="false" placeholder="Username" />
<input type="password" id="password" name="password" value="" autocomplete="false" spellcheck="false" placeholder="Password" />
<input type="submit" id="submit" name="submit" value="submit" />
</form>
</div>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$database = mysqli_connect("database-ip", "database-username", "database-password", "database-name", "database-port"); //fill in the examples correctly!
$fetch_username = mysqli_query($database, "SELECT * FROM login WHERE username='$username'");
$fetch_password = mysqli_query($database, "SELECT * FROM login WHERE password='$password'");
$check_username = mysqli_fetch_array($fetch_username);
$check_password = mysqli_fetch_array($fetch_password);
if($username = $check_username && $password = $check_password){
echo "loged in";
}
if($username != $check_username || $password != $check_password){
echo "incorrect username or password";
}
?>
</body>
</html>
我真的建议你查看官方的php文档:
我希望这能帮到你(=