我正在尝试创建一个简单的登录页面,用于验证从用户收到的凭据,并创建一个包含我的phpadmin所有记录的表的页面。我在 authlogin.php 文件中遇到了问题。在此页面中,如果用户按下Login.html中的提交按钮并且无法验证凭据(用户名== qqqq和密码==== 1111,是的,它是硬编码的。),页面显示:错误消息,然后是Login.html页面的链接。现在,如果用户已从Login.html页面按下提交并且凭据有效,则页面:创建一个HTML表,其中包含在phpadmin中创建的数据库表中的所有行,并将其显示给用户。
我说的麻烦在authlogin.php页面中。 if语句没有被执行,因为我的表中嵌入了php代码。如果我要删除:
<?php foreach ($students as $student) : ?>
<tr>
<td><?php echo $student['studentID']; ?></td>
<td><?php echo $student['studentPassword']; ?></td>
</tr>
代码将运行,但不会显示任何表。非常感激任何的帮助。
<h1 id="title">Login Authentication</h1>
<form action="AuthLogin.php" method="GET">
<div id="data">
<label>User ID:</label>
<input type="text" placeholder="userid" name="userid"/><br />
<label>Password:</label>
<input type="password" placeholder="**********" name="password"/><br />
</div>
<div id="buttons">
<label> </label>
<input type="submit" name="SubmitCredentials" value="Submit"/>
</div>
</form>
<?php
require 'database.php';
$query = 'SELECT * FROM razzam';
$razzam = $db->query($query);
$userid = $_GET['userid'];
$password = $_GET['password'];
if ($userid == 'qqqq' && $password == '1111'):
echo 'Hello '.$userid;
?>
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>PVA</title>
</head>
<body>
<table>
<tr>
<td align = "center"> STUDENT DATA </td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<th>User Name</th>
<th>Password</th>
</tr>
<?php foreach ($students as $student) : ?>
<tr>
<td><?php echo $student['studentID']; ?></td>
<td><?php echo $student['studentPassword']; ?></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<?php
elseif (empty($userid) && empty($password)):
include('Login.html');
echo 'You have not entered a value!';
else:
echo 'Sorry, incorrect credentials entered!';
echo "<p><a href=Login.html>Try Again</a></p>";
endif;
?>
<?php
$dsn = 'mysql:host=localhost;dbname=itec3020';
$username = 'root';
$password = 'root';
try {
$db = new PDO ($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>PVA</title> <!-- check this? -->
</head>
<body>
<h1>Database Error</h1>
<p>There was an error connecting to the database.</p>
<p>The database must be installed as described in appendix A.</p>
<p>The database must be running as described in chapter 1.</p>
<p>Error message: <?php echo $error_message; ?></p>
</body>
</html>
答案 0 :(得分:0)
不要忘记像这样关闭你的foreach
<?php foreach ($students as $student) : ?>
<tr>
<td><?php echo $student['studentID']; ?></td>
<td><?php echo $student['studentPassword']; ?></td>
</tr>
<?php endforeach;?>
答案 1 :(得分:-2)
我认为您必须使用此语法来运行脚本。 我改变的最重要的想法是为foreach循环添加结束标记。 然后我将echo输出更改为短语法,因为如果你使用短语法,我觉得它更容易阅读。
<?php foreach ($students as $student) { ?>
<tr>
<td><?= $student['studentID']; ?></td>
<td><?= $student['studentPassword']; ?></td>
</tr>
<?php } ?>