MYSQL QUERY并显示输出到html

时间:2017-10-30 19:31:25

标签: php html mysql

我想显示每家公司的总评分,总计将列入特定类别

代码就是这个

if (isset($_POST['search'])){

    $key = $_POST['keyword'];

    $com = mysql_query("
SELECT * 
  FROM company
 WHERE id LIKE '%$key%' 
    OR name LIKE '%$key%' 
    OR address LIKE '%$key%' 
    OR city LIKE '%$key%' 
    OR province LIKE '%$key%' 
    OR region LIKE '%$key%' 
    OR country LIKE '%$key%' 
    OR description LIKE '%$key%'
 ");
    while($comshow = mysql_fetch_array($com)){
        $comshowid = $comshow['id'];

        echo '
    <form  method="POST">   
    <div class = "row">
<div class = "text-right">                      
    <b><input  name = "reviewid" style = "margin-right:50%;" class = "text-right" id = "id" type = "text" value = "'.$comshow['id'].'" readonly></b>                    
</div>
</div>
<div class = "row" style = "margin-left:1%;">
    <div class ="col-md-4">
        <img src = "'.$comshow['dp'].'" style = "width:80%;">
    </div>
    <div class = "col-md-6">

        <h2><i>'.$comshow['name'].'</i></h2>
        <hr />
        <p>'.$comshow['description'].'</p>
        <h4><b>Ratings</b></h4>
        <p>Recruitment:</p>
        <p>Tenure:</p>
        <p>Separation:</p>  
        <input class = "btn btn-info" type = "submit"  name ="review" value = "REVIEW">
    </div>                      
</div><br><br></form>

    ';

    }

这是评级表 enter image description here

这是公司表 enter image description here 那是输出 enter image description here

1 个答案:

答案 0 :(得分:0)

@Barmar是对的。您需要使用评级表加入公司表。我还建议使用名为ratings_id的列,这是rating.id的外键。然后,您可以在评级表中有一个名为rating_value的列。

插入以下内容:

    ALTER TABLE ‘rating’ ADD ‘rating_value’ INT NULL;
    ALTER TABLE ‘company’ ADD ‘ratings_id’ INT NULL;

    SELECT c.id, c.name, c.address, c.city, c.province, 
    c.region, c.country, c.description, c.ratings_id, 
    r.id, r.rating_value 
    FROM company c 
    JOIN rating r
    ON c.ratings_is = r.id
    AND
    c.id LIKE '%$key%' 
    OR c.name LIKE '%$key%' 
    OR c.address LIKE '%$key%' 
    OR city LIKE '%$key%' 
    OR c.province LIKE '%$key%' 
    OR c.region LIKE '%$key%' 
    OR c.country LIKE '%$key%' 
    OR c.description LIKE '%$key%';

我强烈建议您在面临SQL注射风险之前,先查看PDO准备语句。