无法在php中连接表和获取数组

时间:2014-01-13 01:18:42

标签: php mysql sql arrays

我想问一下使用MYSQL连接多个数组和获取数组。

这是我的PHP代码。

<?php
session_start();
$con = mysql_connect("localhost", "root", "1234");
mysql_select_db("info2301", $con);

$query = mysql_query("SELECT c.section AS section, c.venue AS venue, c.time AS time, c.lect_name AS lect_name1, c.staff_no AS staff_no1, s.lect_name AS lect_name2, s.staff_no AS staff_no2, s.credit AS credit, s.password AS password
                      FROM class c INNER JOIN lecturer s ON c.staff_no = s.staff_no");

echo "<table border = '1'>
        <tr>
            <th>SECTION</th>
            <th>VENUE</th>
            <th>TIME</th>
            <th>LECTURER NAME</th>
            <th>STAFF NO</th>
        </tr>";

while($row = mysql_fetch_array($query)){
    echo "<tr>";
    echo "<td>". $row['section'] ."</td> ";
    echo "<td>". $row['venue'] ."</td>";
    echo "<td>". $row['time'] ."</td>";
    echo "<td>". $row['lect_name1'] ."</td>";
    echo "<td>". $row['staff_no1'] ."</td>";
    echo "</tr>";
}

echo "</table>";?>

<html>
<script type="text/javascript">
    function ConfirmDelete(){
        var d = confirm('You only can have 2 section');
        return false;
    }

function ConfirmApprove(){
    var d = confirm('The section has been taken');
    return false;
    }
</script>

<body>
    <form action = "lecturer_subject_page.php" method = "POST">
        <br><br>Please select the section you want to register.<br>
        <select name = "section">
            <option value = "first">Section 1</option>
            <option value = "second">Section 2</option>
            <option value = "third">Section 3</option>
            <option value = "fourth">Section 4</option>
            <option value = "fifth">Section 5</option>
            <option value = "sixth">Section 6</option>
        </select>

        <input type = "submit" name = "add" value = "Add Section">

        <?php
            extract($_REQUEST);

            $password = $_SESSION['password'];
            $result = mysql_fetch_array($query);

            if(isset($add)){
                if(isset($section) && $section == "first"){
                    $add_section = mysql_query("UPDATE class SET lect_name1 = '".$result['lect_name2']."', staff_no1 = '".$result['staff_no2']."' WHERE password = '$password'");
                    $plus = $result['credit'] + 3;
                    $add_section = mysql_query("UPDATE lecturer SET credit = '$plus' where password = '$password'");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }

                else if(isset($section) && $section == "second"){
                    $add_section = mysql_query("UPDATE class SET lect_name1 = '".$result['lect_name2']."', staff_no1 = '".$result['staff_no2']."' WHERE password = '$password'");
                    $plus = $result['credit'] + 3;
                    $add_section = mysql_query("UPDATE lecturer SET credit = '$plus' where password = '$password'");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }

                else if(isset($section) && $section == "third"){
                    $add_student = mysql_query("UPDATE student SET section = 3 WHERE password = '$password'");
                    $plus = $result['section'] + 1;
                    $add_section = mysql_query("UPDATE class SET student_number = '$plus' where section = 3");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }

                else if(isset($section) && $section == "fourth"){
                    $add_student = mysql_query("UPDATE student SET section = 4 WHERE password = '$password'");
                    $plus = $result['section'] + 1;
                    $add_section = mysql_query("UPDATE class SET student_number = '$plus' where section = 4");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }

                else if(isset($section) && $section == "fifth"){
                    $add_student = mysql_query("UPDATE student SET section = 5 WHERE password = '$password'");
                    $plus = $result['section'] + 1;
                    $add_section = mysql_query("UPDATE class SET student_number = '$plus' where section = 5");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }

                else{
                    $add_student = mysql_query("UPDATE student SET section = 6 WHERE password = '$password'");
                    $plus = $result['section'] + 1;
                    $add_section = mysql_query("UPDATE class SET student_number = '$plus' where section = 6");
                    $SESSION['password'] = $password;
                    header('Refresh: 0; URL = logout_page.php');
                }
            }

        ?>
    </form>
</body>

当我尝试运行它时,它会显示警告:mysql_fetch_array()要求参数1为资源,第18行和第62行给出布尔值。

我试图找到错误,但似乎无法找到它。我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个,当mysql返回错误时它会死掉:

$query = mysql_query("SELECT c.section AS section, c.venue AS venue, c.time AS time, c.lect_name AS lect_name1, c.staff_no AS staff_no1, s.lect_name AS lect_name2, s.staff_no AS staff_no2, s.credit AS credit, s.password AS password
                      FROM class c INNER JOIN lecturer s ON c.staff_no = s.staff_no") or die(mysql_error());