php中的函数内部函数调用无法正常工作

时间:2013-06-06 05:32:01

标签: php

此代码是一个在php中调用函数的函数。永远不会调用函数调用。

function saveSubject(){
    $result = mysql_query("select * from term where description='".$_POST['term']."'");
    $row = mysql_fetch_array($result, MYSQL_NUM);
    global $term;
    $term = $row[0];
    $x=1;
    while(isset($_POST['subCode'.$x])and isset($_POST['subTitle'.$x]) and isset($_POST['subUnit'.$x])){
            $code = $_POST['subCode'.$x];
            $title = $_POST['subTitle'.$x];
            $unit = $_POST['subUnit'.$x];
            $query = "INSERT INTO subject(subcode, description, units, termid)
                VALUES('".$code."','".$title."',".$unit.",".$term.")";
            $result = mysql_query("SELECT * from subject where subcode='".$code."'");
            if(mysql_num_rows($result) > 0){
                $message = "Subject Code : ".$code;
                prompt($message);
            }else{
                mysql_query($query);
                savePre($code, $x);
                }
            $x++;
        }
}
function savePre($code, $y){
$pre = mysql_query("SELECT subject.subcode from subject left join term
                    on term.termid=subject.termid
                    left join curriculum on term.termid = curriculum.curriculumid
                    where term.courseid =".$_POST['course']);
    while($row = mysql_fetch_array($pre, MYSQL_NUM)){
        $c = $row[0].$y;
        if(isset($_POST[$c])){
            $result = mysql_query("Select * from pre_requisite where pre_requisites=".$row[0]."and subject=".$code);
            if(mysql_num_rows($result) > 0){
                $message = "";
            }else{
                mysql_query("INSERT into pre_requisites(pre_requisite, subject)
                    values (".$row[0].", ".$code.")");
            }
        }
    }
}

在saveSubjec()中调用函数savePre()但调用不起作用。我找不出有什么问题。请帮忙!

2 个答案:

答案 0 :(得分:1)

...简单

您的代码是

$query = "INSERT INTO subject(subcode, description, units, termid)
    VALUES('".$code."','".$title."',".$unit.",".$term.")";

$result = mysql_query("SELECT * from subject where subcode='".$code."'");
if(mysql_num_rows($result) > 0)
{
    $message = "Subject Code : ".$code;
    prompt($message);
}else{
    mysql_query($query);
    savePre($code, $x);
}

从上面的代码中你可以想象你正在向database插入记录,然后使用subcode匹配where condition选择该记录,因此它总是以1返回output 1}}所以else condition 永远不会执行

这就是您无法调用savePre功能的原因。

答案 1 :(得分:0)

您想在saveSubject()函数上定义savePre()函数。使用它。

function savePre($code, $y)
{
 $pre = mysql_query("SELECT subject.subcode from subject left join term
                on term.termid=subject.termid
                left join curriculum on term.termid = curriculum.curriculumid
                where term.courseid =".$_POST['course']);
 while($row = mysql_fetch_array($pre, MYSQL_NUM))
 {
    $c = $row[0].$y;
    if(isset($_POST[$c]))
    {
        $result = mysql_query("Select * from pre_requisite where pre_requisites=".$row[0]."and subject=".$code);
        if(mysql_num_rows($result) > 0){
            $message = "";
        }else{
            mysql_query("INSERT into pre_requisites(pre_requisite, subject)
                values (".$row[0].", ".$code.")");
        }
    }
 }
}

function saveSubject()
{
 $result = mysql_query("select * from term where description='".$_POST['term']."'");
 $row = mysql_fetch_array($result, MYSQL_NUM);
 global $term;
 $term = $row[0];
 $x=1;
 while(isset($_POST['subCode'.$x])and isset($_POST['subTitle'.$x]) and isset($_POST['subUnit'.$x]))
 {
        $code = $_POST['subCode'.$x];
        $title = $_POST['subTitle'.$x];
        $unit = $_POST['subUnit'.$x];

        $result = mysql_query("SELECT * from subject where subcode='".$code."'");
        if(mysql_num_rows($result) > 0){
            $message = "Subject Code : ".$code;
            prompt($message);
        }
        else
        {
           $query = "INSERT INTO subject(subcode, description, units, termid)
            VALUES('".$code."','".$title."',".$unit.",".$term.")";
            mysql_query($query);

            savePre($code, $x);
            }
        $x++;
   }
}