尝试连接两个表时mysqli语法错误

时间:2014-09-10 05:47:27

标签: php mysql join mysqli

我有3个表女巫被称为学生和考试和exam_register考试表包含有关考试的信息,exam_register包含有关在exam_register注册一些考试的学生的信息我有student_id作为外键我需要返回学生谁签署了特定考试的数据,所以我尝试了这个

    <?php 
    if (isset($_POST['submit_exam'])){
        $gerayesh_id=mysqli_real_escape_string($cnn,$_POST['gerayesh_id']);
        $exam_number=mysqli_real_escape_string($cnn,$_POST['exam_number']);
        $query="SELECT s.name , s.lname , s.student_id , e.exam_number e.exam_id FROM";
        $query.=" exam e JOIN exam_register r on e.exam_id=r.exam_id";
        $query.=" JOIN student s on s.student_id=r.student_id";
        $query.=" WHERE (e.gerayesh_id=$gerayesh_id)";
        $query.=" AND (e.exam_number=$exam_number)";
        $result=mysqli_query($cnn,$query);
        if($result){
        echo "<form method=\"post\" action=\"mange_add_result.php\" style=\"float:right;\">";
    echo "<table >";
        echo    "<tr>";
                echo "<th>&#1606;&#1575;&#1605;</th>";
                echo "<th>&#1606;&#1575;&#1605; &#1582;&#1608;&#1575;&#1606;&#1608;&#1575;&#1583;&#1711;&#1740;</th>";
                echo"<th>&#1588;&#1605;&#1575;&#1585;&#1607; &#1570;&#1586;&#1605;&#1608;&#1606;</th>";
                echo "<th>&#1575;&#1606;&#1578;&#1582;&#1575;&#1576;</th>";


            echo"</tr>";
    while($row=mysqli_fetch_array($result) ){

            echo"<tr>";
            echo"<td>{$row['s.name']}</td>";
            echo "<td>{$row['s.lname']}</td>";
            echo "<td>{$row['e.exam_number']}</td>";
            echo "<td><input type=\"checkbox\" value=\" {$row['s.student_id']}\" name=\"student_id\"/></td>";


            echo"</tr>";        

     }
     $row=mysqli_fetch_array($result);

     echo"</table>";
     //echo "<input type=\"hidden\" name=\"exam_id\" value=\"$row['e.exam_id']\">";
      echo "<input type=\"submit\" name=\"submit\" value=\"&#1570;&#1662;&#1604;&#1608;&#1583; &#1705;&#1575;&#1585;&#1606;&#1575;&#1605;&#1607;\">";
     echo "</form>";



        } else{echo mysqli_error($cnn);}


    }

?>

但我得到错误并且说

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.exam_id FROM exam e JOIN exam_register r on e.exam_id=r.exam_id JOIN student s ' at line 1

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在您的查询中,逗号丢失。

$query="SELECT s.name , s.lname , s.student_id , e.exam_number e.exam_id FROM";
                                                              ^

将其更改为

$query="SELECT s.name , s.lname , s.student_id , e.exam_number, e.exam_id FROM";

答案 1 :(得分:0)

你忘了在e.exam_number之后和e.exam_id之前使用逗号(,)对字段进行saparate。