gpt4 book ai didi

php - 我知道我的表包含行,但我的查询返回结果为 0。如何修复此问题?

转载 作者:行者123 更新时间:2023-11-29 18:54:20 24 4
gpt4 key购买 nike

这是一项学校作业,我就是不知道哪里出了问题。一些帮助将不胜感激。我的代码是 PHP 和 HTML 的混合体,它利用表单将条目输入到数据库的表中。表单正常工作并且正在输入条目,但是当我执行返回条目函数以查看表中的所有条目时,没有任何返回。

这是我迄今为止正在使用的代码:

<?php

if(isset($_POST['submit'])){

//getting timezone data for registration timestamp
$timezone = date_default_timezone_set('America/New_York');

//define username and passowrd according to what user entered
$userFName = $_POST['fname'];
$userLName = $_POST['lname'];
$userCity = $_POST['city'];
$userEmail = $_POST['email'];
$regDate = date(format,timestamp);

// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$conn = mysqli_connect("localhost", "root", "root");
//$conn2 = mysqli_connect("localhost", "root", "root", "progAssignment2");

//to protect mySQL injection for Security purposes
$userFName = stripslashes($userFName);
$userFName = mysqli_real_escape_string($conn, $userFName);
$userLName = stripslashes($userLName);
$userLName = mysqli_real_escape_string($conn, $userLName);
$userCity = stripslashes($userCity);
$userCity = mysqli_real_escape_string($conn, $userCity);
$userEmail = stripslashes($userEmail);
$userEmail = mysqli_real_escape_string($conn, $userEmail);

//select my desired database
$db = mysqli_select_db($conn, 'progAssignment2');

//if user submits form, insert new data into table and echo success
$sql = "INSERT INTO MyGuests (firstname, lastname, city, email, reg_date)
VALUES ('$userFName', '$userLName', '$userCity', '$userEmail', '$regDate');";

if (mysqli_query($conn, $sql)) {
echo "Your form has been successfully submitted!";
} else {
echo "Error updating record: " . mysqli_error($conn);
};

//selecting data from mySQL database
$query = "SELECT * FROM MyGuests";
$result = mysqli_query($conn, $query);

};
?>

<!DOCTYPE html>
<html>
<head>
<title>Programming Assignment 3 - Isaiah Duncan</title>
</head>
<body>

<header></header>

<nav></nav>

<section>

<h1>Insert Data Form</h1>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

First Name:<br>
<input type="text" name="fname" value="" required/><br><br>

Last Name:<br>
<input type="text" name="lname" value="" required/><br><br>

City:<br>
<input type="text" name="city" value="" required/><br><br>

Email:<br>
<input type="text" name="email" value="" required/><br><br>

<input type="submit" name="submit" value="Submit" />

</form>

</section>

<section>

<h1>Results</h1>

<?php

//check if (more than zero) rows are returned. if so, loop through and display
if (mysqli_num_rows($result) > 0) {
//output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"] . " - FirstName: " . $row["firstname"] . " - LastName: "
. $row["lastname"] . " - City: " . $row["city"] . " - Email: " . $row["email"] . "<br>";
};
}else{
echo "0 results" . mysqli_error($conn);
};

?>

<?php echo "There are " . mysqli_num_rows($result); ?>

</section>

<?php mysqli_close($conn); ?>

</body>
</html>

最佳答案

据我所知,我认为您想查看您第一次访问该页面时先前插入的所有记录。如果是这种情况,您将需要在 if 语句之外获取 $result。该 if 语句仅在您提交表单时执行。例如,在 if 语句之外(其正下方),输入以下内容:

 $conn = mysqli_connect("localhost", "root", "root");
$db = mysqli_select_db($conn, 'test');
$query = "SELECT * FROM MyGuests";
$result = mysqli_query($conn, $query);

每次访问该页面时都应该运行上述查询。如果有任何结果,应该列出它。

关于php - 我知道我的表包含行,但我的查询返回结果为 0。如何修复此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44219532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com