PHP:如何从数据库中获取数据? 更新 用户名 电子邮件 编辑
代码中第三行有问题PHP:如何从数据库中获取数据? 更新 用户名 电子邮件 编辑,php,sql,Php,Sql,代码中第三行有问题 <?php session_start(); $_SESSION['id']="id"; ?> <!DOCTYPE html> <html> <head> <title>Update</title> </head> <body> <table border="2"> <tr> <th>Username</
<?php
session_start();
$_SESSION['id']="id";
?>
<!DOCTYPE html>
<html>
<head>
<title>Update</title>
</head>
<body>
<table border="2">
<tr>
<th>Username</th>
<th>Email</th>
<th>Edit</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","telephasic");
$q2="select * from register where id = '".$_SESSION['id']."'";
$run=mysqli_query($conn, $q2);
while($row=mysqli_fetch_array($run))
{
$name=$row[1];
$email=$row[2];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><a href="edit.php"> Edit </a></td>
</tr>
<?php } ?>
</table>
</body>
您将$\u SESSION['id']设置为字符串“id”,这样您就不会从数据库中获得任何信息,因为您没有id=“id”的行。删除这一行,一切都会正常运行
$_SESSION['id']="id";
更新
用户名
电子邮件
编辑
跳过while部分,因为您正在获取单个记录。会出现什么错误@Arun SharmaSo你没有得到任何数据,或者数据根本就没有出现?您是否检查了
$row[1]
是否有效并包含数据?为什么$row[1]…
?我想你需要像$row['name']
…我这样做了,但也没有使用$\u会话['id']=“id”;这是什么“id”?但我无法获取行信息使用mysqli_fetch_row不是第23行的mysqli_fetch_数组,并将第二部分放入while循环中,但它只能获取第一个id的数据。我必须获取个人登录的数据。请在$SESSION['id']变量中简单地分配用户id。我建议在用户登录时创建会话id,并使用该会话变量。我已经编辑了代码。
<?php
session_start();
$id = $_SESSION['id'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Update</title>
</head>
<body>
<table border="2">
<tr>
<th>Username</th>
<th>Email</th>
<th>Edit</th>
</tr>
<?php
$conn = mysqli_connect("localhost","root","","telephasic");
$q2 = "select * from register where id = $id ";
$run = mysqli_query($conn, $q2);
$row = mysqli_fetch_array($run);
?>
<tr>
<td><?= $row['name']; ?></td>
<td><?= $row['email']; ?></td>
<td><a href="edit.php"> Edit </a></td>
</tr>
</table>
</body>