下面是我的代码。当我尝试插入一个已经存在的用户时,查询返回一行并且脚本工作正常。但是当查询返回零行时,脚本会在下面的代码中注释为“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 ";
?>