我在屏幕上打印数据库记录时遇到问题。插入值没有问题。它还告诉我们行数。
你能否告诉我取出和打印记录有什么问题
<!DOCTYPE html>
<html>
<body>
<form action="zain.php" method="post">
Topic: <input type="text" name="topic"><br />
<br />
Name: <input type="text" name="name"><br /><br />
Attendance: <input type="text" name="attendance"><br />
<br />
<input type="reset" name="reset">
<input type="submit" name="submit" value="Go">
</form>
<?php
$user = 'root';
$password = 'zz224466';
$db = 'Zain';
// Create connection
$conn = mysqli_connect('localhost', $user, $password, $db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "";
mysqli_select_db($conn, "zain");
//$sql = "CREATE TABLE Lectures(Topic varchar(20), Name varchar(20), Attendence int)";
$sqli = "INSERT INTO lectures(Topic , Name , Attendence) VALUES('$_POST[topic]','$_POST[name]','$_POST[attendance]')";
mysqli_query($conn, $sqli);
////////////////////////// For print DATABASE on the screen ////////////////////////////////////////
if(isset($_POST['submit'])) {
mysqli_select_db($conn, "zain");
$dataBase = "SELECT * FROM lectures";
$print = mysqli_query($conn, $dataBase);
while ($record = mysqli_fetch_array($print)) ;
{
echo $record['Topic'];
echo "</br>";
}
}
mysqli_close($conn);
/////// It tells the number of records in database///////
$countRow = mysqli_num_rows($print);
echo "<br>" . $countRow;
?>
</body>
</html>
答案 0 :(得分:3)
你在while
循环结束时看到了分号吗?
while ($record = mysqli_fetch_array($print)) ;
^ right there
它实际上被认为是有效的语法,不会抛出错误。因此检查它的错误无济于事。
参考文献:
与在C或Perl中一样,PHP要求指令在每个语句的末尾以分号终止。 PHP代码块的结束标记自动表示分号;你不需要一个分号来终止PHP块的最后一行。块的结束标记将包括紧接着的新行(如果存在)。
此外,
您目前的代码向SQL injection开放。使用mysqli_*
with prepared statements或PDO与prepared statements。