简体   繁体   中英

Check if record already exists before creating a new record

I have the following code which posts from the form to the database by saving what is typed into the form. This works but i would like to know how can i be able to check if the record i am entering already exists before adding it as a dublicate and coursing an error. In this case it should be checking if the student_id already exists. If it exists it should echo (record already exists)

$error1='Add New Intern ';
$error0='No error';

    if(isset($_POST['btnaddint']))
{
    $student_id = trim($_POST['student_id']);
    $comp_name = trim($_POST['comp_name']);
    $comp_supervisor = trim($_POST['comp_supervisor']);
    $comp_tel = trim($_POST['comp_tel']);
    $comp_address = trim($_POST['comp_address']);
    $comp_city = trim($_POST['comp_city']);
    $intake_date = trim($_POST['intake_date']);
    $ass_status = trim($_POST['ass_status']);

    if($student_id == '' || $comp_name == '' || $comp_supervisor == '' || $comp_tel == '' || $comp_address == '' || $comp_city == '' || $intake_date == '' || $ass_status == '')
    {
    $error1=" ERROR - Please make sure all required fields are filled ";

    }
else
    {
    require("server/db.php");
    $tbl_name="int_company"; // Table name 
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $student = mysql_query("INSERT INTO $tbl_name (student_id, comp_name, comp_supervisor, comp_tel, comp_address, comp_city, intake_date, ass_status) VALUES('".$student_id."','".$comp_name."','".$comp_supervisor."','".$comp_tel."','".$comp_address."','".$comp_city."','".$intake_date."','".$ass_status."')") or die("Query failed:4 ".mysql_error());
    $error1=" Record has been added... ";
    }
}

try this

 <?php
    $error1='Add New Intern ';
    $error0='No error';

    if(isset($_POST['btnaddint']))
    {
        $student_id = trim($_POST['student_id']);
        $comp_name = trim($_POST['comp_name']);
        $comp_supervisor = trim($_POST['comp_supervisor']);
        $comp_tel = trim($_POST['comp_tel']);
        $comp_address = trim($_POST['comp_address']);
        $comp_city = trim($_POST['comp_city']);
        $intake_date = trim($_POST['intake_date']);
        $ass_status = trim($_POST['ass_status']);

        if($student_id == '' || $comp_name == '' || $comp_supervisor == '' || $comp_tel == '' || $comp_address == '' || $comp_city == '' || $intake_date == '' || $ass_status == '')
        {
            $error1=" ERROR - Please make sure all required fields are filled ";

        }
        else
        {
            require("server/db.php");
            $tbl_name="int_company"; // Table name 
            // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
            mysql_select_db("$db_name")or die("cannot select DB");

            $res_student = mysql_query("SELECT student_id FROM $tbl_name WHERE student_id='$student_id' LIMIT 1 ") or die(mysql_error());
            if($row_student = mysql_fetch_assoc($res_student))
            {
                $error1 = "Record is already exists ... ";
            }
            else
            {
                $student = mysql_query("INSERT INTO $tbl_name (student_id, comp_name, comp_supervisor, comp_tel, comp_address, comp_city, intake_date, ass_status) VALUES('".$student_id."','".$comp_name."','".$comp_supervisor."','".$comp_tel."','".$comp_address."','".$comp_city."','".$intake_date."','".$ass_status."')") or die("Query failed:4 ".mysql_error());
                $error1=" Record has been added... ";
            }   
        }
    }

?>

This question has been already asked in stackoverflow, you can view this one here Logic for already exist record check but only in case of updated form values and using conditional logic : check if record exists; if it does, update it, if not, create it . These above link will help you to get answer. I guess this will help you

If the student id field is a UNIQUE index then trying to add the same id will fail.

You can then trap the error and if it is due to an existing record, display whatever you want.

However if you are learning PHP programming, you should not be using mysql_ as it is deprecated - either use mysqli or PDO

 $student_id =mysql_real_escape_string( trim($_POST['student_id']));
$res = mysql_query('select count(*) from $tbl_name where student_id= ' .$student_id) or die();
$row = mysql_fetch_row($res);
if ($row[0] > 0)
{
    //student_id exists
}
else
{
    //It doesn't
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM