Q:

Auto-fill input with data from Mysql database

Hi, I am using a JQuery script to fill 3 fields on my form when an ID is selected. My script pulls data from 3 different PHP files and it is making my script run slowly. I would like to know if there is a way to compile all of my PHP files into 1 file to make my script more convenient. 
script
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';

}

?>

A:

Change your name.php script so that it does the following:
<?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);
}

?>

Then change your javascript to:
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']);
    });
}

