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

            } 

        }

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

            } 

}
?>

it worked...thank you Bachov Varghese..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.