tags:

views:

326

answers:

2
if(isset($_REQUEST['SAVE'])) {
    $sql = "SELECT SecName FROM section WHERE SecID='$section'";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)){
        $SecName = $row["SecName"];
    }
    $sql = "SELECT SubjName FROM subject WHERE SubjID='$subject'";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)){
        $SubjName = $row["SubjName"];
    }

    $check = mysql_query("SELECT SecID FROM service where SubjID='$SubjName'")or die(mysql_error());
    $bool = 1;
    while($info = mysql_fetch_array( $check )) 
        if(($info['SecID'] == $SecName)){
            $bool = 0;
        }
    if($bool){

        $check = mysql_query("SELECT * FROM service ")or die(mysql_error());
        $bool = 1;
        while($info = mysql_fetch_array( $check )) 
            if(($info['Start_date'] == $Start_date) && ($info['Venue'] == $Venue) || ($info['Start_date'] == $Start_date) && ($info['Facilitator'] == $Facilitator)){
                $bool = 0;
            }
        if($bool){

            $sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID)VALUES('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";



            if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
            echo '<script type="text/javascript">';
            echo 'alert("Save Successfully!");';
            echo 'window.location="Admin_RecSchedMapLst.php";';
            echo '</script>';
            mysql_close($con);
        }else
            {       
                echo '<script type="text/javascript">';
                echo 'alert("Conflicting schedule for Venue or Facilitator!");';
                echo '</script>';
            }                       
    }else
        {   
            echo '<script type="text/javascript">';
            echo 'alert("The SECTION has been already scheduled!");';
            echo '</script>';
        }                   
}                   

/*  $result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')"; */                          

}

this code is working correctly,. i just want to ask how can i possibly work on the code whcih is commented above. all i want to do is when the serviceid is inserted i want also to immediately insert the serviceID, idno, type to another table which is registered.

+1  A: 

Execute another SQL INSERT command after the first one, like:

$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";

if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}
else
{
    /*Begin your SELECT and INSERT code*/
    $result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
    $row = mysql_fetch_assoc($result);
    $SIDno = $row['SIDno'];
    $result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
    $row = mysql_fetch_assoc($result);
    $ServiceID = $row['ServiceID'];
    $sql2 = "INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";   
    /*End your SELECT and INSERT code*/                      

    if(!mysql_query($sql2,$con))
    {
        die('Error: ' . mysql_error());
    }
}
lc
just like the code that is commented above?
Oh, I get it...yeah basically. Just put it after the first INSERT.
lc
...and execute it like my example.
lc
but the forgot to tell you that the idno number is needed to be selected from another table named stude.
Yeah I figured that out, you can use your code to select, and put it in the else clause. I'll edit this for you.
lc
does this code will get all the student that are enrolled for particular subject and section in table class? and insert it all in the registered table?
No, you're only gonna get the first one...you'll have to fetch each row individually and insert them in a loop.
lc
so how can i merge that code with the code insert?
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/retrieve-data-from-a-mysql-database.aspx and instead of echoing something, do your insert inside the loop.
lc
thank you sir this will really help me a lot,. its for my senior project im graduating this march so im so busy coz i need to finish doing my project.
Well, good luck, and watch out for those SQL injection attacks as Nathan mentioned. You might want to look at the rest of that tutorial site as well if you get stuck again.
lc
http://stackoverflow.com/questions/596283/dont-display-the-past-schedule-activities -- pls check also this one
+2  A: 

It's much easier and better to use the mysql_insert_id function to get the ID of the row you just inserted, rather than doing another SELECT. E.g.:

$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";

if (mysql_query($sql,$con)) {
   $ServiceID = mysql_insert_id();
   $sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";           
   mysl_query($sql, $con);  // check for error here too though
}
else {
   echo "OH NO!!!";
}

Also, beware of SQL injection with the way you're building your SQL strings.

Nathan
He actually doesn't have to do that. He just inserted the new ServiceID as $RecCode. Certainly worth mentioning though, but I think he's looking for a different ServiceID than the one he just inserted (or I'm just very confused)?
lc
I may be confused too, and uncertain to boot, but I'm not sure...
Nathan
the same serviceId will be inserted but the IDno is needed to be select also from a another table named class. and get all the idno there and insert it to another table named registered including the serviceid
Very good point with the SQL injection. Didn't even think to mention it.
lc
It's been mentioned to him before: http://stackoverflow.com/questions/595689/how-can-i-get-the-values-of-from-the-checkboxes-and-insert-it-into-a-db and http://stackoverflow.com/questions/595689/how-can-i-get-the-values-of-from-the-checkboxes-and-insert-it-into-a-db
John Rasch