显示和更新MySQL数据库中的数据

时间:2016-08-08 20:04:22

标签: php html mysql

所以这就是我想要做的。我正在尝试获取已在MySQL数据库中输入的信息并将其显示在php文件中。使用下面我包含的代码,我可以查看我登录的一个用户的数据,但是一旦我注销并使用其他用户登录,旧用户信息仍会显示,则新用户信息没有更新。我还确保通过创建一个注销按钮来结束会话,我也在下面列出了这个按钮。

<?php
 session_start();
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE ";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

?>

我的HTML片段

<html>

div class="form-group">

   <label for="exampleInputEmail1">Email address</label>
   <input type="email" class="form-control" placeholder="Email" value="<?php echo $row["email"]; ?>">

                                            </div>


<html>

退出php代码

<?php
session_start();
session_destroy();
header("Location: index.php");
?>

2 个答案:

答案 0 :(得分:0)

将您的SQL查询更改为:

$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE username = <variable_having_user_name_value>";

如果是其他标识符而不是用户名(如电子邮件或身份证):

$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE < identifier > = <variable_having_identifier_value>";

答案 1 :(得分:-1)

$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE ";
$result = $conn->query($sql);

SQL中没有WHERE数据。所以我猜MySQL总是给你相同的用户。

您需要提供以下条件:

// Get your user's profile data (from session for exemple)
$email = $_SESSION['email'];
$passcode = your_cypher_function($_SESSION['passcode']);
// Then complete your query
$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE email = $email AND passcode = $passcode";

不要忘记保护您的数据不受SQL注入的影响。我给了你一个简单的例子来快速测试。