从下拉框中的选择中返回mysql查询

时间:2013-09-09 01:13:03

标签: php mysql sql

您好我已经使用3个表的mysql查询在表单上创建了一个下拉组合框,这工作正常,现在我想在数据库中执行另一个查询,当我在下拉组合中选择一个项目时在数据库上运行查询并返回在定义的字段中使用其他数据填充表单所需的值。 以下是我到目前为止的距离......

$query3 = "SELECT customers.id, customers.email, customers_customfields.customer_id, customers_customfields.field_value, orders.sign_date, orders.sub_id, orders.domain_name, orders.cust_status 
FROM customers, customers_customfields, customers_orders, orders 
WHERE customers_orders.order_id = orders.sub_id
AND customers.id = customers_orders.customer_id 
AND customers.id = customers_customfields.customer_id
AND customers_customfields.field_id = 1";

$result = mysql_query ($query3);
echo "<select name=orders value='Select from the list provided '>";


while($drop=mysql_fetch_array($result)){

//data stored in $drop
echo "<option value=$drop[id]>$drop[sub_id]&nbsp;&nbsp;&nbsp;&nbsp;$drop[id]&nbsp;&nbsp;$drop[field_value]&nbsp;&nbsp;$drop[sign_date]&nbsp;&nbsp;$drop[domain_name]&nbsp;&nbsp;&nbsp;&nbsp;$drop[cust_status]</option>";

}
echo "</select>";        

2 个答案:

答案 0 :(得分:0)

PHP严格来说是一种服务器端语言。如果您想让数据库中的字段动态更新用户的应用程序(无需提交和刷新),那么您将不得不使用AJAX。

阅读一些AJAX教程以获得一般的想法。

此外,使用数据库抽象层(如PDO)也是一个好主意。

它几乎适用于任何环境,允许使用预准备语句以及其他好东西。另外,使用MySqli代替不推荐使用的MySql。

Ajax教程:http://www.smashingmagazine.com/2008/10/16/50-excellent-ajax-tutorials/

PDO:http://php.net/manual/en/book.pdo.php

准备好的陈述:http://php.net/manual/en/pdo.prepared-statements.php

MySqli:http://php.net/manual/en/book.mysqli.php

答案 1 :(得分:0)

你确实应该使用AJAX和PHP。

例如,首先在下拉列表中设置一个jQuery.change触发器,如下所示:

$(document).ready(function () {

    $('#yourDropDown').change(function (e) {
        yourFunction($(this).val());
    });
});

function yourFunction(inputString) {
        $.ajax({

            url: "../folder/yourPHP.php",
//The inputString will contain the value of your DropDown, you will give this parameter in the querystring
            data: 'queryString=' + inputString,
            success: function (msg) {
//MSG will be the "message" from the query
//As you see you'll put the MSG in your HTML of your #yourFormTag
//This message can contain everything you want
                if (msg.length > 0) {
                    $('#yourFormTag').html(msg);

                }
            }
        });
}

只是一个.PHP文件示例:

<?php
if (isset($_REQUEST['queryString'])) {

    include '../yourConnection.php';



    $inputString = $_GET['queryString'];

    $select_query = "SELECT <yourColumns>

            FROM <yourTable>
            WHERE <Where you want to check if inputString is the same> LIKE CONCAT('%', :inputString, '%')
            ORDER BY <order if you want? ASC";

    $get = $db->prepare($select_query);

    $get->execute(array(':inputString' => $inputString);

    //For example to fill a select
    echo '<select>';

    while ($row = $get->fetch(PDO::FETCH_ASSOC)) {
        echo "<option value='" . $row['yourColumn'] . "'>" . $row['yourColumn'] . "</option>";
    }

    echo '</select>';

}
?>

正如我所说.PHP文件就是一个例子。

希望这有帮助,干杯!