gpt4 book ai didi

php - 使用 Web 表单中的单个用户输入查询数据库

转载 作者:行者123 更新时间:2023-11-30 00:20:58 24 4
gpt4 key购买 nike

我使用以下代码查询我的数据库,但页面显示空白!

我看不出问题出在哪里!请帮忙。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Extract Student Data</title>
</head>
<body bgcolor="white">
<?php
include 'db.inc';

// Show all student data in a <table>
function displayStudentsList($connection,$query,$Year)
{
// Run the query on the DBMS
if (!($result = @ mysql_query ($query, $connection)))
showerror( );

// Find out how many rows are available
$rowsFound = @ mysql_num_rows($result);

// If the query has results ...
if ($rowsFound > 0)
{
// ... print out a header
echo "Students data Of Year $Year<br>";

// and start a <table>.
echo "\n<table>\n<tr>" .
"\n\t<th>Admission No.</th>" .
"\n\t<th>Last Name</th>" .
"\n\t<th>First Name</th>" .
"\n\t<th>Gender</th>" .
"\n\t<th>Orphan</th>" .
"\n\t<th>District</th>\n</tr>";

// Fetch each of the query rows
while ($row = @ mysql_fetch_array($result))
{
// Print one row of results
echo "\n<tr>" .
"\n\t<td>" . $row["Admission"] . "</td>" .
"\n\t<td>" . $row["last_name"] . "</td>" .
"\n\t<td>" . $row["first_name"] . "</td>" .
"\n\t<td>" . $row["Gender"] . "</td>" .
"\n\t<td>" . $row["Orphan"] . "</td>" .
"\n\t<td>" . $row["District"] . "</td>" .
"\n</tr>";
} // end while loop body

// Finish the <table>
echo "\n</table>";
} // end if $rowsFound body

// Report how many rows were found
echo "$rowsFound records found matching your
criteria<br>";
} // end of function

$scriptName = "combined.php";

// Has the user provided the parameter?
if (empty($Year))
{
// No, the user hasn't provided a parameter
?>
<form action="<?=$scriptName;?>" method="GET">
<br>Enter Year Of Admission :
<input type="text" name="Year" value="All">
(type All For All Years)
<br>
<input type="submit" value="Show Data">
</form><br>
<a href="index.html">Home</a>
<?php
} // end of if empty($Year) body
else
{
// Secure the user parameter $Year
$Year = clean($Year, 30);

// Connect to the MySQL DBMS
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect");

if (!mysql_select_db($databaseName, $connection))
showerror( );

// Start a query ...
$query = "SELECT Admission,
last_name,
first_name,
Gender,
Orphan,
District
FROM osasaasasaalumni_index;"

// ... then, if the user has specified a year,
// add the Year as an AND clause ...
if ($Year != "All")
$query .= " AND year = \"$Year\"";

// ... and then complete the query.
$query .= " ORDER BY last_name";

// run the query and show the results
displayStudentsList($connection, $query, $Year);

// Close the DBMS connection
mysql_close($connection);
} // end of else if empty($Year) body
?>
</body>
</html>

我的 db.inc 是

<?
$hostName = "xxxxxx";
$databaseName = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
?>

代码有什么问题,因为当我调用它时,它显示一个空白页面?

最佳答案

您缺少;在本声明的末尾:

原文:

$query = "SELECT   Admission,
last_name,
first_name,
Gender,
Orphan,
District
FROM osasaasasaalumni_index;"

更新:

$query = "SELECT   Admission,
last_name,
first_name,
Gender,
Orphan,
District
FROM osasaasasaalumni_index;";

如果您看到空白页且没有错误,则应打开错误报告。例如,可以通过将此代码放在脚本的开头、<?php 之后来完成。 :

error_reporting(E_ALL);
ini_set('display_errors', '1');

关于php - 使用 Web 表单中的单个用户输入查询数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23237507/

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