我能够使用PHP创建一个MySQL数据库,用我的数据填充它,然后在我的网页上弹出它。但是,我希望用户能够从Options DropDown中点击Football中的位置,比如QuarterBack,然后网页将只显示团队中四分卫的球员。
我非常确定我需要的是与我的SELECT * FROM
编辑 - 此外,我在网页上的代码显示了Unidentified Index - Position,即使我定义了它
相关代码信息在此处:
<form action="FinalProject.php" method="get">
<select name="position" id="position" value="position">
<option value="">Select a position:</option>
<option value="1">Quarterback</option>
<option value="2">Wide Receiver</option>
<option value="3">Linebacker</option>
<option value="4">Tight End</option>
</select>
</form>
<?php
// --- CREATE THE DATABASE
$db_conn = mysqli_connect("localhost", "root", "");
if (!$db_conn)
die("Unable to connect: " . mysqli_connect_error());
mysqli_query($db_conn, "CREATE DATABASE IF NOT EXISTS sportDatabase;");
// --- CREATE THE TABLE
mysqli_select_db($db_conn, "sportDatabase");
$cmd = "CREATE TABLE bearsRoster (
number int(2) NOT NULL PRIMARY KEY,
playerName varchar(20),
position char(2),
height varchar(5),
weight int(3),
age int(2),
experience int(2),
collegeName varchar(50)
);";
mysqli_query($db_conn, $cmd);
$cmd = "LOAD DATA LOCAL INFILE 'bearsRoster.csv' INTO TABLE bearsRoster FIELDS TERMINATED BY ',';";
mysqli_query($db_conn, $cmd);
$selectedOption = $_GET["position"];
$cmd = "SELECT * FROM bearsRoster ";
// ORDER BY position WHERE".$selectedOption.";";
$records = mysqli_query($db_conn, $cmd);
echo("<table border = 'black' align:'center'>
<tr>
<th>Number</th>
<th>Player Name</th>
<th>Position</th>
<th>Height</th>
<th>Weight</th>
<th>Age</th>
<th>Experience</th>
<th>College Name</th>
</tr>" . PHP_EOL);
while ($row = mysqli_fetch_array($records)) {
echo("<tr>
<td id = 'yellow'>" . $row['number'] . "</td>
<td id = 'red'>" . $row['playerName'] . "</td>
<td id = 'red'>" . $row['position'] . "</td>
<td id = 'blue'>" . $row['height'] . "</td>
<td id = 'blue'>" . $row['weight'] . "</td>
<td id = 'pink'>" . $row['age'] . "</td>
<td id = 'pink'>" . $row['experience'] . "</td>
<td id = 'pink'>" . $row['collegeName'] . "</td>
</tr>" . PHP_EOL);
}
echo("</table>");
mysqli_close($db_conn);
?>
答案 0 :(得分:0)
我编辑了你的代码,我应该这样做。
<form action="FinalProject.php" method="POST">
<select name="position" id="position" value="position">
<option value="">Select a position:</option>
<option value="QB">Quarterback</option>
<option value="WR">Wide Receiver</option>
<option value="LB">Linebacker</option>
<option value="TE">Tight End</option>
</select>
<input type="submit" name="submit" value="Click me!" />
</form>
<?php
$db_conn = mysqli_connect("localhost", "root", "");
if (!$db_conn)
die("Unable to connect: " . mysqli_connect_error());
mysqli_query($db_conn, "CREATE DATABASE IF NOT EXISTS sportDatabase;");
mysqli_select_db($db_conn, "sportDatabase");
$cmd = "CREATE TABLE bearsRoster (number int(2) NOT NULL PRIMARY KEY, playerName varchar(20), position char(2), height varchar(5), weight int(3), age int(2), experience int(2), collegeName varchar(50));";
mysqli_query($db_conn, $cmd);
$cmd = "LOAD DATA LOCAL INFILE 'bearsRoster.csv' INTO TABLE bearsRoster FIELDS TERMINATED BY ',';";
mysqli_query($db_conn, $cmd);
$cmd = "SELECT * FROM bearsRoster";
if(isset($_POST['submit'])) {
if($_POST['position'] == 'QB' || $_POST['position'] == 'WR' || $_POST['position'] == 'LB' || $_POST['position'] == 'TE')
$selectedOption = $_POST['position'];
else
$selectedOption = null;
if($selectedOption !== null)
$cmd = "SELECT * FROM bearsRoster WHERE position = ".$selectedOption.";";
}
$records = mysqli_query($db_conn, $cmd);
echo("<table border = 'black' align:'center'><tr><th>Number</th><th>Player Name</th><th>Position</th><th>Height</th><th>Weight</th><th>Age</th><th>Experience</th><th>College Name</th></tr>" . PHP_EOL);
while($row = mysqli_fetch_array($records)){
echo("<tr><td id = 'yellow'>" . $row['number'] . "</td><td id = 'red'>" . $row['playerName'] . "</td><td id = 'red'>" . $row['position'] . "</td><td id = 'blue'>" . $row['height'] . "</td><td id = 'blue'>". $row['weight'] . "</td><td id = 'pink'>". $row['age'] . "</td><td id = 'pink'>". $row['experience'] . "</td><td id = 'pink'>". $row['collegeName'] . "</td></tr>". PHP_EOL);
}
echo("</table>");
mysqli_close($db_conn);
答案 1 :(得分:0)
在表创建中使用IF NOT EXISTS
,因为每次运行脚本时都会删除并创建表。您可以使用AJAX从服务器获取数据,而无需重新加载页面或重定向。
AJAX是开发人员的梦想,因为你可以:
- 更新网页而不重新加载页面
- 在页面加载后从服务器请求数据
- 在页面加载后从服务器接收数据
- 将数据发送到服务器 - 在后台
在select
添加onchange属性中,该属性将调用Javascript函数,该函数将从数据库中获取数据并在页面上显示而不重新加载。
<强> HTML 强>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<select name="position" id="position" onchange="getPosition(this)" value="position">
<option value="">Select a position:</option>
<option value="QB">Quarterback</option>
<option value="WR">Wide Receiver</option>
<option value="LB">Linebacker</option>
<option value="TE">Tight End</option>
</select>
<div id="results"></div>
<script>
function getPosition(position) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("results").innerHTML = this.responseText;
}
};
xhttp.open("POST", "FinalProject.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('position=' + position.value);
}
</script>
</body>
</html>
<强> FinalProject.php 强>
<?php
// --- CREATE THE DATABASE
$db_conn = mysqli_connect("localhost", "root", "");
if (!$db_conn)
die("Unable to connect: " . mysqli_connect_error());
mysqli_query($db_conn, "CREATE DATABASE IF NOT EXISTS sportDatabase;");
// --- CREATE THE TABLE
mysqli_select_db($db_conn, "sportDatabase");
$cmd = "CREATE TABLE IF NOT EXISTS bearsRoster (
number int(2) NOT NULL PRIMARY KEY,
playerName varchar(20),
position char(2),
height varchar(5),
weight int(3),
age int(2),
experience int(2),
collegeName varchar(50)
);";
mysqli_query($db_conn, $cmd);
$cmd = "LOAD DATA LOCAL INFILE 'bearsRoster.csv' INTO TABLE bearsRoster FIELDS TERMINATED BY ',';";
mysqli_query($db_conn, $cmd);
$selectedOption = $_POST["position"];
$cmd = "SELECT * FROM `bearsRoster` WHERE position = '$selectedOption' ORDER BY position";
$records = mysqli_query($db_conn, $cmd);
if ($records && mysqli_num_rows($records)) : ?>
<table border='black' align="center">
<tr>
<th>Number</th>
<th>Player Name</th>
<th>Position</th>
<th>Height</th>
<th>Weight</th>
<th>Age</th>
<th>Experience</th>
<th>College Name</th>
</tr>
<?php while ($row = mysqli_fetch_array($records)) : ?>
<tr>
<td id='yellow'><?php echo $row['number']; ?></td>
<td id='red'><?php echo $row['playerName']; ?></td>
<td id='red'><?php echo $row['position']; ?></td>
<td id='blue'><?php echo $row['height']; ?></td>
<td id='blue'><?php echo $row['weight']; ?></td>
<td id='pink'><?php echo $row['age']; ?></td>
<td id='pink'><?php echo $row['experience']; ?></td>
<td id='pink'><?php echo $row['collegeName']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php else: ?>
<h1>No Data found</h1>
<?php endif;
mysqli_close($db_conn);
?>