0

This code is a function calling a function in php. The function call is never called.

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.")");
            }
        }
    }
}

Calling function savePre() in saveSubjec() but the calling is not working. I cannot find out what is wrong. Please help!

6
  • 2
    because its not executing else statement..make sure mysql_num_rows($result) return 0 then your function will work Commented Jun 6, 2013 at 5:33
  • When you say the call is not working, what do you mean? Are you getting an error? Is the database updating? Try adding this to the end of every mysql_query() or die mysql_error(); Commented Jun 6, 2013 at 5:34
  • echo something before function call to see if the else executing or not. Commented Jun 6, 2013 at 5:34
  • write the savePre function above of the saveSubjec and then try. Commented Jun 6, 2013 at 5:35
  • @DipeshParmar I have done that. The query before that call is executing well. Commented Jun 6, 2013 at 5:36

2 Answers 2

1

Simple...

You code is

$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);
}

from above code you can imagine that you are inserting record to database and then selecting that record using subcode match where condition so it will always return 1 as output so your else condition will never get execute.

That's the reason why you are not able to call savePre function.

Sign up to request clarification or add additional context in comments.

2 Comments

he does not execute insert query before select.
ohhh Yes i did not realized it. +1 for u
0

You want to define savePre() function above the saveSubject() function. Use this.

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++;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.