使用下拉菜单从MySQL中提取和发布数据

时间:2016-01-07 03:06:39

标签: php html mysql menu dropdown

我是php& amp;的新手MySQL的 基本上我想要做的是能够从表中检索数据 在MySQL数据库中,并将其放在下拉菜单中。 在我填写其他字段后,我想从下拉菜单中将数据写入同一数据库中的另一个表。

这是我的insert.php文件

        <?php

        #### INSERTS A SINGLE CUSTOMER IN Company-->Customer Database with UTF8 Change for special German Characters  - Cyrilic Doesnt work - Other change then utf8 ???
        #### Getting Data from Index.php 


        $servername = "127.0.0.1";
        $username = "root";
        $password = "";
        $dbname = "company";    

        //making an array with the data recieved, to use as named placeholders for INSERT by PDO.


        #### Getting DAta from Index.php 
        $data = array('CustomerName' => $_POST['CustomerName'] , 'Address1' => $_POST['Address1'], 'Address2' => $_POST['Address2'], 'City' => $_POST['City'], 'PostCode' => $_POST['PostCode'], 'CountryID' => $_POST['CountryID'], 'ContactName' => $_POST['ContactName'], 'ContactEmail' => $_POST['ContactEmail'], 'ContactPhone' => $_POST['ContactPhone'], 'ContactFax' => $_POST['ContactFax'], 'Website' => $_POST['Website']);

        try {
            // preparing database handle $dbh
            $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); ### Database Connect with Special characters Change 
            // set the PDO error mode to exception
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            // query with named placeholders to avoid sql injections
            $query = "INSERT INTO customer (CustomerName, Address1, Address2, City, PostCode, CountryID, ContactName, ContactEmail, ContactPhone, ContactFax, Website ) 
            VALUES (:CustomerName, :Address1, :Address2, :City, :PostCode, :CountryID, :ContactName, :ContactEmail, :ContactPhone, :ContactFax, :Website )";
            //statement handle $sth
            $sth = $dbh->prepare($query);
            $sth->execute($data);
            echo "New record created successfully";
            }
        catch(PDOException $e)
            {
            echo $sql . "<br>" . $e->getMessage();
            }
        $dbh = null;
        ?>

这是来自html页面的来源

        <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Add New Product</title>
        </head>
        <body>
        <form action="insert.php" method="post">
            <p>
                <label for="CustomerName">CustomerName:</label>
                <input type="text" name="CustomerName" id="CustomerName">
            </p>
            <p>
                <label for="Address1">Address 1:</label>
                <input type="text" name="Address1" id="Address1">
            </p>
            <p>
                <label for="Address2">Address 2:</label>
                <input type="text" name="Address2" id="Address2">
            </p>

            <p>
                <label for="City">City:</label>
                <input type="text" name="City" id="City">
            </p>

            <p>
                <label for="PostCode">Post Code:</label>
                <input type="text" name="PostCode" id="PostCode">
            </p>

            <p>
                <label for="CountryID">Country ID:</label>
                <input type="text" name="CountryID" id="CountryID">
            </p>

            <p>
                <label for="ContactName">Contact Name:</label>
                <input type="text" name="ContactName" id="ContactName">
            </p>

            <p>
                <label for="ContactEmail">Contact Email:</label>
                <input type="text" name="ContactEmail" id="ContactEmail">
            </p>

            <p>
                <label for="ContactPhone">Contact Phone:</label>
                <input type="text" name="ContactPhone" id="ContactPhone">
            </p>

            <p>
                <label for="ContactFax">Contact Fax:</label>
                <input type="text" name="ContactFax" id="ContactFax">
            </p>

            <p>
                <label for="Website">Website:</label>
                <input type="text" name="Website" id="Website">
            </p>

            <input type="submit" value="Add Records">
        </form>
        </body>
        </html>

所以我想添加这个PHP代码,它会拉出国家/地区列表和电话号码的前缀,然后当我点击提交按钮时,下拉菜单的实际输出将写入客户表中

                <p>
                    <label for="CountryID">Country:</label>

                        <?php
                        $servername = "localhost";
                        $username = "root";
                        $password ="";
                        $dbname = "company";

                        $con_qnt = mysqli_connect($servername, $username, $password, $dbname);      
                        if(!mysqli_connect("localhost","root",""))
                        {
                            die('oops connection problem ! --> '.mysqli_connect_error());
                        }
                        if(!mysqli_select_db($con_qnt, "company"))
                        {
                            die('oops database selection problem ! --> '.mysqli_connect_error());
                        }


                        $sql = "SELECT * FROM country";
                        $result = mysqli_query($con_qnt, "SELECT * FROM country" );


                        echo "<select name='label'>";
                        while ($row = mysqli_fetch_array($result)) {
                        echo "<option value='" . $row['CountryName' ] . "'>" . $row['CountryName' ] . "     ("  .$row['PhonePrefix' ] . ")"  . "</option>";

                            } 
                        echo "</select>";
                        ?>          
                    <name="CountryID" id="CountryID">
                </p>

我甚至不知道这是否可行 - 我搜索了很长时间但找不到任何我需要的东西。大多数情况下,我发现了硬编码的html下拉菜单。事实上,硬编码将适用于各国,但如果我希望它能够在下拉菜单中显示让我们说产品,它就无法工作。谢谢大家 。

2 个答案:

答案 0 :(得分:0)

您好我刚刚做了一个快速示例,回答了有关如何使用数据库中的数据填充下拉列表的问题。 在这个例子中,我使用了

  

JQuery的AJAX功能

首先,对于后端,(这只是选择数据的快速示例。请勿将此作为未来代码的模式使用

if(choice.get() == 1) {......}

我再说一遍。这只是为了快速参考。不要将它用作代码的模式。 因此,使用该代码,我们现在已从表国家/地区选择数据,并使用<?php $dbh = new PDO("mysql:host=localhost;dbname=dbhelp", 'root', 'pup'); $stmt = $dbh->prepare("SELECT * from t_country"); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($result); ?> 以json格式制作数据。在这里阅读更多相关信息。 http://php.net/manual/en/function.json-encode.php

下一个是前端(HTML部分)

json_encode

答案 1 :(得分:0)

如果我找对你,你想要替换它:

<p>
   <label for="CountryID">Country ID:</label>
   <input type="text" name="CountryID" id="CountryID">
</p>

通过下拉菜单,您可以从下拉列表中快速选择国家/地区,而不是记住每个国家/地区的ID。要创建select,您正在使用此代码:

<p>
    <label for="CountryID">Country:</label>

    <?php

        // some code here that I removed to avoid noise

        $sql = "SELECT * FROM country";
        $result = mysqli_query($con_qnt, "SELECT * FROM country" );

        echo "<select name='label'>";
        while ($row = mysqli_fetch_array($result)) {
           echo "<option value='" . $row['CountryName' ] . "'>" . $row['CountryName' ] . "     ("  .$row['PhonePrefix' ] . ")"  . "</option>";
        } 
        echo "</select>";
    ?>

    <name="CountryID" id="CountryID">
</p>

在解决一些问题后,这应该可以正常工作:

  1. 如果您要将input替换为selectselect应与input同名,以便自动处理该值。现在select有“标签”作为名称,并且有一个奇怪的(和不正确的)name标签。解决这个问题:

    • 摆脱不必要的name标记。

    • name中的select属性替换为值“CountryID”:

      echo "<select name='CountryID'>";
      
  2. option值应为国家/地区ID,因此,除非ID是国家/地区名称(不是最佳选择),否则您使用的值不正确。也改变那条线:

    echo "<option value='" . $row['CountryID' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";
    

    (将CountryID视为ID,将其替换为正确的列名称。)

  3. 我认为通过这两个更改,其余的代码应该可以正常使用insert.php,根本不需要任何更新。最后它看起来像这样:

    <p>
        <label for="CountryID">Country:</label>
    
        <?php
    
            // some code here that I removed to avoid noise
    
            $sql = "SELECT * FROM country";
            $result = mysqli_query($con_qnt, "SELECT * FROM country" );
    
            echo "<select name='CountryID'>";
            while ($row = mysqli_fetch_array($result)) {
               echo "<option value='" . $row['CountryID' ] . "'>" . $row['CountryName' ] . "     ("  .$row['PhonePrefix' ] . ")"  . "</option>";
            } 
            echo "</select>";
        ?>
    </p>