PHP Dropdown mySQL 两张表

标签 php mysql

带有工作代码的编辑版本:

表架构:

类别(catid,catname)

产品(productid,catid,产品名)

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

if(isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
//get the id
$productid = (int) $_GET['id'];
} else {
    echo 'error'; }

$sql = $link->prepare("SELECT p.productid, p.catid pid, p.productname, c.catid cid, c.catname FROM product p
JOIN category c
WHERE p.productid = $productid");
$sql-> execute();
$result = $sql-> fetchall(PDO::FETCH_ASSOC);
    $option = '';
foreach ($result as $row) {
    $pid = $row['pid'];
    $productname = $row['productname'];
    $catid = $row['cid'];
    $catname = $row['catname'];
    $option .= '<option name="'.$catname.'" value="'.$catid.'" '.($pid==$catid ? 'selected="selected"' : '').'>'.$catname.'</option>'."\r\n";
}

然后在页面中:

<select name="xxx">
<? echo $option ?>
</select>

最佳答案

在您的 foreach 循环中,您只执行一个查询而不迭代结果集数据。如果不是,请解释您的 query() 方法。

关于PHP Dropdown mySQL 两张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11463819/

相关文章:

java - 如何在PHP服务器上显示android POST字符串?

php - 将图像存储在实时网站的文件系统中

mysql - 错误 2005 (HY000) : Unknown MySQL server host '[35.232.51.216]' (0) in google cloud SQL client

mysql - 简单的数据库查看器和搜索工具

php - 在 PHP 中动态显示/隐藏 Div

php - PHP 中单引号和双引号字符串有什么区别?

php - 为 Youtube 评论投票(外部)

mysql - 使用 Hadoop 更新 MySQL

mysql - mysql使用连接池时为什么需要释放连接?

mysql - MySQL WHERE子句中的子查询使用