我有一个下拉列表,它是从MYSQLi查询中填充的。我希望用户选择一个选项,并从数据库中提取相关的值/记录,并根据FirstName显示给用户。
我的表没有id列(我没有做任何。以FirstName开头)
它显示带有列标题的表,但它不会从db中动态获取数据,这恰好在下面的[view code]中的while循环中编码。 我花了一整夜的时间试图解决这个问题。帮助:(
列表代码
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<form name="form_update" method="post" action="update_test.php">
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//This creates the drop down box
echo "<select name= 'FirstName'>";
echo '<option value="">'.'--- Please Select Person ---'.'</option>';
//$query=mysqli_query($con,"SELECT id,FirstName FROM persons");
$query = mysqli_query($con,"SELECT FirstName FROM persons");
//$query_display = mysqli_query($con,"SELECT * FROM persons");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['id']."'>".$row['FirstName']
.'</option>';
}
echo '</select>';
?> <input type="submit" name="submit" value="Submit"/>
</form>
<br/><br/>
<a href="main.html"> Go back to Main Page </a>
</body>
</html>
查看代码
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
if(isset($_POST['FirstName']))
{
$name = $_POST['FirstName'];
$fetch="SELECT Firstname FROM persons WHERE Firstname = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{
echo "Error:".(mysqli_error($con));
}
//display the table
echo '<table border="1">'.'<tr>'.'<td align="center">'. 'From Database'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td>'.'First Name'.'</td>'.'<td>'.'
LastName'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'. 'Subject'. '</td>'.'<td>'.'
Hobbies' .'</td>'.'</tr>';
//Supposed to collect data from db-I tried using _array,_assoc instead of _row
//and got mysqli_result()
// requires one parameter given boolean
while($data=mysqli_fetch_row($result))
{
echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td> <td>$data[4] </td></tr>");
}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
答案 0 :(得分:2)
您正在检查'FirstName'后期数据,这是SELECT的值。但是,此值将是其中一个OPTION的值,该值设置为ID。您应该将FirstName作为OPTION的值。
此外,您当前没有从数据库中检索ID。
答案 1 :(得分:0)
我意识到我已经使用了PDO而不是mySqli。抛开这些琐事,如果我理解了你的问题,这应该足以让你走上正轨。
如果您运行它,您会注意到客户lastName的唯一显示时间是对表单提交的响应,其中他们是所选公司。
<强> testQuery.php 强>
<?php
// check if the name of the select box is present in the POST variables. if so, the form is being submitted.
// If not, it's a fresh load of the page (the page has been requested by something other than itself)
if (isset($_POST["selectedCustomer"]))
$formSubmitted = true;
else
$formSubmitted = false;
// connect to the mysql server
$pdo = null;
try
{
$userName = 'root'; // enter the appropriate values HERE
$password = ''; // and HERE
$pdo = new PDO('mysql:host=localhost', $userName, $password);
}
catch (PDOException $e)
{
die('Can\'t establish a connection to the database: ');
}
// create a database to answer thisa forum question
$pdo->query("create database forumQuestion");
$pdo->query("use forumQuestion");
// create a table
$queryStr = "CREATE TABLE IF NOT EXISTS `customer` (`id` int(11) NOT NULL AUTO_INCREMENT, `firstName` varchar(128) NOT NULL, `lastName` varchar(128) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ";
$pdo->query($queryStr);
// insert some data
$queryStr = "INSERT INTO `customer` (`id`, `firstName`, `lastName`) VALUES (1, 'ILM 3d studios', 'Industrial Light & Magic'), (2, 'Airwalk clothing', 'Why walk on the ground?');";
$pdo->query($queryStr);
?>
<!doctype html>
<html>
<head>
</head>
<body>
<?php if ($formSubmitted == true)
{
$submittedCustomerId = $_POST["selectedCustomer"];
$queryStr = "select * from customer where id = :id";
$params = array(":id" => $submittedCustomerId);
$query = $pdo->prepare($queryStr);
$query->execute($params);
$onlyResult = $query->fetch();
printf("Form has been submitted.<br>");
printf("id(%d), firstName(%s), lastName(%s)", $submittedCustomerId, $onlyResult["firstName"], $onlyResult["lastName"]);
}
else
printf("Form awaiting submission");
?>
<hr>
<h2>Select a ccustomer</h2>
<form action="testQuery.php" method="POST">
<select name='selectedCustomer'>
<option value='0'>-- Select a customer --</option>
<?php
$queryStr = "select * from customer";
$query = $pdo->prepare($queryStr);
$query->execute();
while ($curCustomer = $query->fetch())
{
printf("<option value='%d'>%s</option>", $curCustomer["id"], $curCustomer["firstName"]);
}
?>
</select>
<input type='submit' value='submit form'/>
</form>
</body>
</html>