Php下拉菜单通过MySQL显示数据

时间:2015-05-23 08:56:56

标签: php mysql database

我使用http://www.databasejournal.com/scripts/practice-sql.html中的脚本安装了MySQL数据库:

然后我从Youtube视频中学习了我的PHP代码,了解如何使用SQL中的数据填充下拉列表,但是单击“显示详细信息”提交按钮时,仍然无法解决问题。我还是PHP的新手,无法自己解决。谢谢!!

// PHP代码

<?php 
require'require.php';

$usersQuery="
    SELECT DISTINCT
        c.cno,
        c.cname,
        o.eno,
        o.shipped
    FROM customers c
    RIGHT JOIN orders o
    ON c.cno=o.cno
    group by (c.cname)

    ";


    $users=$db->query($usersQuery);

    if(isset($_GET['user'])){
        $userQuery="
            {$usersQuery}
            WHERE c.cno=:cno";

        $user= $db->prepare($userQuery);
        $user->execute(['cno'=>$_GET['user']]);
        $selectedUser=$user->fetch(PDO::FETCH_ASSOC);

    }

?>
<!DOCTYPE html> 
<html lang="en">
<head>

<title>Dropbox</title>
<script language=JavaScript>

</script>

</head>
<body>
<h3>My Dropdown</h3>

<form action="dropbox.php" method="get">
    <select name="user">
        <option value="">Choose one</option>
    <?php  foreach($users->fetchAll() as $user):?>
        <option value="<?php echo $user['cno'];?>" <?php echo isset($selectedUser)&& $selectedUser['cno']==$user['cno']? "selected":""?> > <?php echo $user['cname'];?> </option>

    <?php endforeach ?>

</select>

<input type="submit" value="Show details" >
</form>
<?php if(isset($selectedUser)):?>
    <pre><?php echo($selectedUser['cno']);?></pre>
    <?php endif; ?>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

执行语句有问题。你忘记了:,你必须将数组传入它。它将是

 $user->execute(array(":cno" =>$_GET['user']));
 $selectedUser=$user->fetch(PDO::FETCH_ASSOC);

阅读execute

答案 1 :(得分:-1)

这是一个有效的代码。

我试图评论我从你的代码

所做的所有修改

但首先让我们看看你所做的查询:

SELECT DISTINCT c.cno, c.cname, o.eno, o.shipped
FROM customers c
RIGHT JOIN orders o
ON c.cno=o.cno
 group by (c.cname)
  

DISTINCT和GROUP BY @strawberry表示并不喜欢同一个查询。   在此查询中,GROUP BY子句&#34;将合并&#34;查询BY cname的结果。

原文:

  

但是,假设我们输入了2个具有相同名称的客户端(它可能的主要原因是cdo),并且这两个客户都订购了一些东西。你不会在一个不是主键的列上使用GROUP BY来错过一个。

     

GROUP BY的最佳方式是PRIMARY KEY。

     

顺便说一下Ur变量名称可能很棘手(比如$ users&amp; $ user)

Original queries variables :

$usersQuery =  "SELECT c.cno, c.cname
            FROM customers c, orders o
            WHERE c.cno = o.cno
            GROUP BY c.cno";
AND

$userQuery = "SELECT *
                  FROM customers
                  WHERE cno = :cno";

编辑:请参阅Strawberry的评论(下方)

GROUP BY VS DISTINCT

  

我误以为如何构建查询,所以我以这种方式进行了更改。使用此查询运行了一个更好的过程(为头脑):

"SELECT DISTINCT c.cno, c.cname, c.street, c.zip, c.phone
            FROM orders o
            LEFT JOIN customers c
            ON o.cno = c.cno"

Then add a WHERE clause when one user is returned by the form :

<?php 

// My include of connecting to my DB - same as ur require.php i suppose 
include("./inc.connect.php");


// As said before 
$usersQuery =  "SELECT DISTINCT c.cno, c.cname, c.street, c.zip, c.phone
                FROM orders o
                LEFT JOIN customers c
                ON o.cno = c.cno";

$users = $db->query($usersQuery);

if(isset($_GET['user']))
{
    // This query will return all informations about the user u selected
    // $userQuery="{$usersQuery} WHERE c.cno=:cno"
    // as @saty said u missed ':' but ur string query 
    //  You included 2 clause WHERE 
    // (from usersQuery and the concatenation) 

    $userQuery = "{usersQuery} WHERE cno = :cno";

    $user = $db->prepare($userQuery);
    $user->execute(array(":cno" => $_GET['user']));

    $selectedUser = $user->fetch(PDO::FETCH_ASSOC);

    // Display the array (<pre> tag make it readable)
    print "<pre>";
    print_r($selectedUser);
    print "</pre>";
}

?>

<!DOCTYPE html> 
<html lang="en">
    <head>
        <title>Dropbox</title>

        <script>
        </script>

    </head>

    <body>
        <h3>My Dropdown</h3>

        <!-- Nothing important : just changed action value coz 
             of my name's page -->
        <form action="index.php" method="get">
            <select name="user">
                <!-- Added "Default" value for first option -->
                <option value="Default">Choose one</option>
                <?php  
                // used echo only to display html tags - 
                // make it cleaner to read
                    foreach($users->fetchAll() as $user)
                    {
                    // Can't make the display Better - SRY
                        echo "<option value=\"" . $user['cno'] . "\"";
                        echo (isset($selectedUser) && 
                             ($selectedUser['cno'] == $user['cno'])) 
                             ? "selected" :"";

                        echo ">" . $user['cname'] . "</option>";
                    }
                ?> 
            </select>
            <input type="submit" value="Show details">
        </form>
        <?php 
            if(isset($selectedUser))
                echo "<pre>" . ($selectedUser['cno']) . "</pre>";
        ?>
    </body>
</html>

希望有所帮助。