gpt4 book ai didi

php - 如何从与php中的dropdownlist相关的数据库中获取值

转载 作者:行者123 更新时间:2023-11-30 00:27:22 26 4
gpt4 key购买 nike

请帮忙解决这个问题。$query1 和 $query 语句没有给出正确的 o/p。$query1 显示 o/p as-->资源 id #5*错误是--> undefined variable :第 30 行 C:\xampp\htdocs\Programs\hosp.php 中的 hname*

我的问题是:编写一个 PHP 脚本,该脚本接受医院名称并以表格格式打印有关在该医院就诊的医生的信息。

我的数据库是:医院(hid,hname)doctor(did,dname,hid)//这里hid是外键

我写的程序:-

<html>
<body>
<form method="post" action="<?php $PHP_SELF;?>">
Select Hospital Name:<br>
<?php
$con = mysql_connect('localhost', 'root');
if (!$con)
{
die("Could not connect..".mysql_error());
}
mysql_select_db('employee');

$sql = "SELECT hname FROM hospital";
$result = mysql_query($sql);

echo "<select name='hname'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['hname'] ."'>" . $row['hname'] ."</option>";
}
echo "</select>";
?>
<br><input type="submit" name="submit" value="submit">
<?
if (isset($_POST['submit']))
{
//echo "Selected value: $_POST[hname]";
$hosp = $_POST['hname'];
echo "Hosp Name: $hosp";
$query1=mysql_query("select hid from hospital where hname='$hname'");
//$query1=mysql_query("select hid from hospital where hname='{$_POST[hname]}' ");
echo $query1;
$query=mysql_query ("SELECT did from doctor where hid='$query1' ");
while( $row = mysql_fetch_array($query))
{
$did=$row['did'];
echo $did;
}
}
?>
</form>
</body>
</html>

最佳答案

if (isset($_POST['submit']))
{
$hname = $_POST['hname'];

//First query
$query1 = "select hid from hospital where hname='".$hname."'";
//echo $query1;
$result=mysql_query($query1);
$row = mysql_fetch_assoc($result);
$hid = $row['hid'];
//echo $hid;

//second query
$query2 = "SELECT did from doctor where hid=".$hid;
//echo $query2;
$result2=mysql_query($query2);
$row2 = mysql_fetch_assoc($result2);
$did = $row['did'];
//echo $did;
}

注意事项:

  • 避免 SQL 注入(inject)
  • 使用 PDO 或 mysqli 扩展

关于php - 如何从与php中的dropdownlist相关的数据库中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22767662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com