使用Mysql数据库中的数据自动填充输入

时间:2014-05-06 01:06:59

标签: php jquery mysql

嗨,当我选择ID时,我正在使用JQuery脚本填充表单上的3个字段。我的脚本从3个不同的PHP文件中提取数据,这使得我的脚本运行缓慢。我想知道是否有办法将我的所有PHP文件编译成1个文件,以使我的脚本更方便。

脚本

function updatefrm($name, $qty1, $price, $plink) {
var name = $("#" + $name).val();
if ($.trim(name) !='') {
    $.post('../action/subs/name2.php', {name: name}, function(data) {
        $("#" + $qty1).val(data);       
});
    $.post('../action/subs/name.php', {name: name}, function(data) {
        $("#" + $price).val(data);  
});

    $.post('../action/subs/name3.php', {name: name}, function(data) {
        $('#cimage').attr('src', '../components/mijoshop/opencart/image  /'+data);      
});

}
}

name.php

<?php
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {

require '../db/connect6.php';

$query = mysql_query("
SELECT `product`.`price` 
FROM `shop_product`
WHERE  `shop_product`.`product_id` = '".  mysql_real_escape_string(trim($_POST['name']))."'
    ");
echo (mysql_num_rows($query)!==0)? mysql_result($query,0,`price`):'price not  found';

}

?>

name1.php

<?php
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {

require '../db/connect6.php';

$query = mysql_query("
SELECT `product`.`product_id` 
FROM `shop_product`
WHERE  `shop_product`.`product_id` = '".     mysql_real_escape_string(trim($_POST['name']))."'
    ");
echo (mysql_num_rows($query)!==0)? mysql_result($query,0,`product_id`):'price not found';

}

?>

name2.php

<?php
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {

require '../db/connect6.php';

$query = mysql_query("
SELECT `product`.`image` 
FROM `shop_product`
WHERE  `shop_product`.`product_id` = '".       mysql_real_escape_string(trim($_POST['name']))."'
    ");
echo (mysql_num_rows($query)!==0)? mysql_result($query,0,`image`):'price not  found';

}

?>

2 个答案:

答案 0 :(得分:0)

您应该使用LIMIT 1通过一个查询请求所有mysql数据(现在您将循环遍历数据库中的所有行3次。然后在填充字段之前将数据作为json发送到您的jquery。< / p>

你可以google for&#34; jquery json php&#34;阅读更多相关内容。

这样的事情:

header('Content-Type: application/json');
$id = mysql_real_escape_string($_POST['name']);
$result = mysql_query("SELECT product_id,price,image FROM shop_product WHERE product_id = $id LIMTI 1") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
   echo json_encode(array('id' => "$row['product_id']", 'price' => "$row['price']", 'image' => "$row['image']"));
}

答案 1 :(得分:0)

更改name.php脚本,以便执行以下操作:

<?php
// empty will do isset too, so you get two for the price of one!
if (!empty($_POST['name'])) {
    require '../db/connect6.php';

    $safe_name = mysql_real_escape_string(trim($_POST['name']));

    $query = mysql_query("
        SELECT price, product_id, image
        FROM `shop_product`
        WHERE  `shop_product`.`product_id` = '". $safe_name ."'
        LIMIT 1
    ");

    if (mysql_num_rows($query) > 0) {
        $row = mysql_fetch_assoc($query);
        json($row);
    } else {
        json(null);
    }
}

function json ($array) {
    header("Content-Type: application/json");
    echo json_encode($array);
}

?>

然后将您的javascript更改为:

if ($.trim(name) !='') {
    $.post('../action/subs/name.php', {name: name}, function(data) {
        $("#" + $qty1).val(data['qty']);
        $("#" + $price).val(data['price']);
        $("#cimage").attr('src', '../components/mijoshop/opencart/image/'+data['image']);
    });
}