0

I need help!!

What I want is when I add a new teacher, it will search the tblteacher_info. If there is already a record of that teacher, it will just update his/her record. If there is no record of that teacher, it will add his/her info in the tblteacher_info.

I have here my codes but it's not working.

$sqlsearch = mysql_query("SELECT * FROM tblteacher_info WHERE Fname='$iFName' AND Mname='$iMName' AND Lname='$iLName'",$open_con);
    if(!$sqlsearch)
        {
            die("Database connection failed:" . mysql_error());
        }


        while ($row = mysql_fetch_array($sqlsearch))
        {

        if (($row ['Fname'] == "$iFName") AND ($row['Mname'] == "$iMName") AND ($row['Lname'] =="$iLName"))
            {
                $i_sqlupdate = mysql_query("UPDATE tblteacher_info SET Fname='$iFName', Mname = '$iMName', Lname = '$iLName', Username ='$username', Password='$password' WHERE Lname = '$iFName' AND Mname ='$iMName' AND  Lname = '$iLName'  ",$open_con);

                if(!$i_sqlupdate)
                    {
                    die("Error in saving: ". mysql_error());
                    }


            }
            elseif (($row ['Fname'] != "$iFName") AND ($row['Mname'] != "$iMName") AND ($row['Lname'] !="$iLName") )
            {
                $i_sqlinsert= mysql_query("INSERT INTO tblteacher_info (Teacher_ID, Fname, Mname, Lname, Username, Password) VALUES ('0', '$iFName', '$iMName', '$iLName', '$username', '$password')",$open_con);

                if(!$i_sqlinsert)
                    {
                    die("Error in saving: ". mysql_error());
                    }

            } 

        }
2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by Mits14
0

Try this

<?php
$sqlsearch = mysql_query("SELECT * FROM tblteacher_info WHERE Fname='$iFName' AND Mname='$iMName' AND Lname='$iLName'",$open_con);
    if(!$sqlsearch)
        {
            die("Database connection failed:" . mysql_error());
        }

else {
            if(mysql_num_rows($sqlsearch)>0) {

                $i_sqlupdate = mysql_query("UPDATE tblteacher_info SET Fname='$iFName', Mname = '$iMName', Lname = '$iLName', Username ='$username', Password='$password' WHERE Lname = '$iFName' AND Mname ='$iMName' AND  Lname = '$iLName'  ",$open_con);

                if(!$i_sqlupdate)
                    {
                    die("Error in saving: ". mysql_error());
                    }
            }

            else
            {
                $i_sqlinsert= mysql_query("INSERT INTO tblteacher_info (Teacher_ID, Fname, Mname, Lname, Username, Password) VALUES ('0', '$iFName', '$iMName', '$iLName', '$username', '$password')",$open_con);

                if(!$i_sqlinsert)
                    {
                    die("Error in saving: ". mysql_error());
                    }

            } 

}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.