这个问题的第一部分让我从文本框中获取一个队长名称,查询数据库并将信息拉入表格。现在我需要创建一个带有队长名称的下拉列表,用名字和姓氏填充它(数据库字段是独立的,所以我需要加入它。我试过爆炸但我无法获得任何东西填充在第一位),然后在用户按下提交按钮时再次显示该表。
重申一点,我无法弄清楚如何:
感谢您的帮助,我会继续尝试解决这个问题。
编辑:我能够使用下面修改后的代码填充下拉列表。我仍然有一个问题,即抓取所选名称并查询下面的数据,因为它已更改为下拉列表而不是用户输入所需名称的文本框。
我收到以下错误:
注意:未定义的索引:第26行的C:\ Users ... \ PhpstormProjects \ test.php中的名称
第26行:$ name = htmlentities($ _ POST ['name']);
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<form action="test.php"method="post">
<input type="submit" name="submit" value="submit">
</form>
<?php
$connection = mysqli_connect("host", "username", "password") or die ("could not connect to mysql");
mysqli_select_db($connection, 'database') or die ("no database");
$captainResults = mysqli_query($connection, "SELECT concat(fname,' ', lname) as capname from captain");
echo '<select name="name">';
while ($row = mysqli_fetch_row($captainResults)){
foreach ($row as $value) {
echo '<option>' . $value. ' </option>';
}
}
echo '</select>';
if(isset($_POST['submit'])) {
$name = htmlentities($_POST['name']);
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$connection = mysqli_connect("mysql553.profrusso.com", "finalexam", "finalexam");
mysqli_select_db($connection, 'shoretoshore');
$result = mysqli_query($connection, "SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'");
echo '<table border="1">
<tr style="font-weight:bold">
<th>Shipment No.</th>
<th>Shipment Id.</th>
<th>Arrival Date</th>
<th>Origin</th>
<th>Destination</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
while ($row = mysqli_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
print "<td>".$value."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
答案 0 :(得分:1)
不确定您是否已经尝试过,但作为建议,使用合适的组合框(也就是:)可能更容易。
<?php
// We're going to need the connection in any case
$connection = mysqli_connect("server", "username", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
echo '<table border="1">
<tr style="font-weight:bold">
<th>Shipment No.</th>
<th>Shipment Id.</th>
<th>Arrival Date</th>
<th>Origin</th>
<th>Destination</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
while ($row = mysqli_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
print "<td>".$value."</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['submit'])) {
$name = htmlentities($_POST['name']);
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
// Make sure to cleanse the input to prevent attacks
$firstname = mysqli_real_escape_string( $connection, $firstname );
$lastname = mysqli_real_escape_string( $connection, $lastname );
// Query the DB for specific captain data
$result = mysqli_query($connection, "SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'");
} else {
// Here is where we'll handle initial page loading
// Query the DB for bootstrapping data
$shipmentResults = mysqli_query($connection, "SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment);
// Use this to populate dropdown (aka: combobox/select) for captain name
$captainResults = mysqli_query($connection, "SELECT * from captain);
// Build the dropdown
echo '<select id="captain_drop_down">';
while ($row = mysqli_fetch_row($captainResults)) {
foreach ($row as $value) {
echo '<option value="' . $row["id"] . '">' . $row["firstname"] . '" "' . $row["lastname"] . '</option>';
}
}
echo '</select>'; // End of #captain_drop_down
}
&GT;
答案 1 :(得分:0)
想出来。感谢Benjamin的帮助
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<form action="webfinal2.php"method="post">
<?php
$connection = mysqli_connect("mysql553.profrusso.com", "finalexam", "finalexam") or die ("could not connect to mysql");
mysqli_select_db($connection, 'shoretoshore') or die ("no database");
$captainResults = mysqli_query($connection, "SELECT concat(fname,' ', lname) as capname from captain");
echo '<select name="name">';
while ($row = mysqli_fetch_row($captainResults)){
foreach ($row as $value) {
echo '<option>' . $value. ' </option>';
}
}
echo '</select>';
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$connection = mysqli_connect("mysql553.profrusso.com", "finalexam", "finalexam");
mysqli_select_db($connection, 'shoretoshore');
$result = mysqli_query($connection, "SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'");
echo '<table border="1">
<tr style="font-weight:bold">
<th>Shipment No.</th>
<th>Shipment Id.</th>
<th>Arrival Date</th>
<th>Origin</th>
<th>Destination</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
while ($row = mysqli_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
print "<td>".$value."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>