0

下面是我的代码。当我尝试插入一个已经存在的用户时,查询返回一行并且脚本工作正常。但是当查询返回零行时,脚本会在下面的代码中注释为“STOPS HERE”的点停止。请我真的不明白这个问题,我是 php 新手。即使是 ifs 之外的 echo 语句也不会执行。

<?php
session_start();
$_SESSION["error"] = "";
include "dbconnection.php";

$_SESSION["validUser"] = 4; 

$validUser = $_SESSION["validUser"];


extract($_POST);

if($courseSession == ""){
    echo "You must Select a course session ";   
}
else {
    $query = "SELECT * FROM courses WHERE student = $validUser AND course='$courseSession'";
    $result = mysql_query($query, $db)  or die(mysql_error($db));

    if(!$result){
        echo " no results have been returned testing "; // used for testing
    }
    else {
        echo " Results returned but don't understand  \n Number rows = ";   // used for testing
        echo mysql_num_rows($result);

    } //STOPS HERE, here is where the script stops after query above returns 0 row.
    $check = mysql_num_rows($result)   or die(mysql_error($db));

    echo " after check course ";
    if($check){

        echo "You have already registered for this session "; 


    }

    else if(!$check){

        $query = "INSERT INTO courses
                            (course, student, groupId)

                        VALUES
                            ('$courseSession', '$validUser', '$group')";

        $res = mysql_query($query, $db) or die(mysql_error($db));
        if(!$res){
            echo "Error Registering Course ";
        }
        else{
            echo "Successfully registered for this session ";
        }
    }
}


echo " TESting testing ";


?>
4

3 回答 3

1

此行的代码存在问题:

$check = mysql_num_rows($result) or die(mysql_error($db));.

当它运行时,mysql_num_rows($result)返回 0,在这种情况下,它的计算结果与 false 相同,这就是为什么die()被执行并且之后没有运行的原因。

于 2013-08-03T12:39:00.400 回答
0

尝试以下可能有助于替代的方法:

$query = "SELECT Count(*) as ResultVal FROM courses WHERE student = $validUser AND course='$courseSession'";
$result = mysql_query($query, $db)  or die(mysql_error($db));

if($result["ResultVal"] > 0){
       echo "You have already registered for this session "; 
   }

    else if($result["ResultVal"] = 0){ //OR just else

        $query = "INSERT INTO courses

        //rest of the code
于 2013-08-03T12:51:03.827 回答
0

尝试 $check = mysql_num_rows($result) 或 die("hi control is here"); 而不是 $check = mysql_num_rows($result) 或 die(mysql_error($db));

如果它打印“hi control is here”,那么这证实 die 是问题所在。

于 2013-08-03T12:56:06.297 回答