dqsa17330 2015-02-17 13:54
浏览 41
已采纳

使用PHP和mysqli从数据库中获取Selectbox值

I want to pick up peoples names from a phpmyadmin database and place them in a HTML select box, when the user picks a name from the select box it should display the detail from the database for that person in a table. I can't seem to get this to work, I can get the names to pick up from the database and display in a select box but when you click on the name it seems to bring up every record in the database rather than just the one for that person. I am using mysql rather than mysql. Here is my code

This is my back end stuff

<?php 
$conn = mysqli_connect("localhost", "root", "root") or die ("No connection"); 
mysqli_select_db($conn, "flat") or die("db will not open"); 
$query = "select FlatCode, Address from FLAT"; 
$result = mysqli_query($conn, $query) or die("Invalid query"); 
echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";  
while($row = mysqli_fetch_array($result)) 
{ 
    echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td></tr>";
} 
echo "</table>"; 
mysqli_close($conn); 
?> 

this is my front end stuff

<font size="4"> Choose an Owner Name</font><br><br> 
<form action="flat.php" method="post"> 
<select name="name"> 
<?php 
$con = mysqli_connect("localhost", "root", "root") or die ("No connection"); 
mysqli_select_db($con , "flat") or die ("db will not open"); 
$query = "SELECT distinct OwnerName, FlatCode, Address from FLAT"; 
/*$query= $_POST ("name")
function change_guery($query)
mysqli_use_result*/

$result = mysqli_query($con, $query) or die("Invalid query");  
while($rows = mysqli_fetch_array($result))

{ 
      echo "<option value=\"" . $rows[0] . "\">" . $rows[0] . "</option>"; 
} 

echo "</select>"; 
           

mysqli_close($con); 
?> 
<input type="submit" value="Submit Value"> 
</form></body></html> 

</div>
  • 写回答

1条回答 默认 最新

  • dsa456369 2015-02-17 14:23
    关注

    There is a problem in your flat.php code. You are posting the the info correctly via form but you forgot to receive it via $_POST in flat.php.

    See the following code and comments in it, it should work -

    <?php 
    $n = $_POST["name"];//we receive the name passed by the form
    $conn = mysqli_connect("localhost", "root", "root") or die ("No connection"); 
    mysqli_select_db($conn, "flat") or die("db will not open"); 
    $query = "select FlatCode, Address from FLAT WHERE `OwnerName` = '$n' LIMIT 1";//see the changes here 
    $result = mysqli_query($conn, $query) or die("Invalid query"); 
    echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";  
    $row = mysqli_fetch_array($result);
    //as the result will return 1 row only so we dont need while loop here
    echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td></tr>";
    echo "</table>"; 
    mysqli_free_result($result);//dont forget to free result
    mysqli_close($conn); 
    ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器