0

感谢您阅读我的问题

我正在尝试使表repair_jobs 中的*clients_id*显示为表contacts中的名称

但我没有运气,我有 2 个 sql 查询,这是错的吗?

第一个

$query = "select * from repair_jobs";

这有助于我显示我需要的有关 repair_jobs 和 works 字段的信息

这是第二个

$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";

在此之下,我有这个尝试显示客户端的名称

echo "<td>{$client_id}</td>";

但它只显示数字而不是我需要的数据(客户名称)

我错过了什么吗?


附加信息

client_id (repair_jobs) 是一个数字,与 id (contacts) 相同,但要显示名称 (contacts)

客户

Id – name – surname – phone – address

维修

Id – clients_id (same as id in clients) – unit – date – price

当前代码

<?php
//include database connection
include 'db_connect.php';

//query all records from the database
$query = "select * from repair_jobs";

//execute the query
$result = $mysqli->query( $query );

//get number of rows returned
$num_results = $result->num_rows;

//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record

    //start table
    //creating our table heading
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
        echo "<th>Job #</th>";
        echo "<th>Name Of Unit</th>";
        echo "<th>Client</th>";
        echo "<th>Estimated Value</th>";
    echo "</thead></tr><tbody><tr>";

    //loop to show each records
    while( $row = $result->fetch_assoc() ){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
                echo "<td>{$rmake} {$rmodel}</td>";

$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";

echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);




print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
    }

    echo "</tr></table>";//end table

}else{
    //if database table is empty
    echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?>
4

2 回答 2

2

将您的第一个查询更改为您加入查询,因为没有理由在您的代码中间进行第二个查询。(你也从来没有执行过那个查询)。

//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";

然后在你的桌子上保留$client_name

echo "<td>{$client_name}</td>";
于 2013-08-25T15:23:51.763 回答
1
<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
    echo "<th>Job #</th>";
    echo "<th>Name Of Unit</th>";
    echo "<th>Client</th>";
    echo "<th>Estimated Value</th>";
    echo "</tr></thead><tbody>";
    while( $row = $result->fetch_assoc() ){
        extract($row);
        echo "<tr>";
        echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
        echo "<td>{$make} {$model}</td>";
        echo "<td>{$client_name}</td>";
        echo "<td align='center'><span class='badge badge-success'>£";
        $lhours = $labour;
        $repaircosts = $ourcosts;
        $labourpay = $labourcharge;
        $sum_total = $repaircosts +($lhours * $labourpay);
        echo $sum_total;
        echo '</span></td>';
        echo "</td>";
        echo "</tr>";
    }
    echo "</tbody></table>";
} else {
    echo "No records found.";
}
$result->free();
$mysqli->close();
?>
于 2013-08-25T15:05:58.130 回答