0

我有一个从 MYSQLi 查询填充的下拉列表。我希望用户选择一个选项,并从数据库中提取相关的值/记录并根据名字显示给用户。

我的表没有 id 列(我没有做任何。以 FirstName 开头)

它显示带有列标题的表,但它不会从数据库中动态获取数据,这些数据恰好是在下面 [查看代码] 的 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>';
  }
 ?>
4

2 回答 2

2

您正在检查 'FirstName' 后数据,它是 SELECT 的值。但是,此值将是设置为 ID 的选项之一的值。您应该将 FirstName 作为 OPTION 的值。

此外,您当前没有从数据库中检索 ID。

于 2013-06-19T21:53:45.383 回答
0

I realize I've used PDO instead of mySqli as you have. With that triviality aside, if I've understood your question, this should be more than enough to put you on the right track.

If you run it, you'll note that the only time the customers lastName is displayed is in response to a form submission, in which they were the chosen company.

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>
于 2013-06-19T22:37:52.237 回答