【问题标题】:Check if record already exists before creating a new record [duplicate]在创建新记录之前检查记录是否已经存在[重复]
【发布时间】:2014-04-21 20:03:06
【问题描述】:

我有以下代码,它通过保存输入到表单中的内容从表单发布到数据库。这可行,但我想知道如何才能检查我输入的记录是否已经存在,然后再将其添加为重复项并引发错误。在这种情况下,它应该检查 student_id 是否已经存在。如果存在它应该回显(记录已经存在)

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

【问题讨论】:

  • 这不是您问题的答案,但您的代码使用服务器端验证,并不是验证表单的有效方法。它需要往返于服务器并返回。通过 javascript 使用客户端验证要好得多。
  • @Steve 这不是“好得多”。并且不应该使用它来代替服务器端验证。
  • 您应该在 student_id 上有一个唯一/主键。之后使用INSERT ... ON DUPLCATE KEY UPDATE查询

标签: php mysql


【解决方案1】:

这个问题已经在stackoverflow中被问过了,你可以在这里查看这个问题Logic for already exist record check but only in case of updated form valuesusing conditional logic : check if record exists; if it does, update it, if not, create it。 以上这些链接将帮助您获得答案。 我想这会对你有所帮助

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:

      如果学生 ID 字段是唯一索引,则尝试添加相同的 ID 将失败。

      然后您可以捕获错误,如果它是由现有记录引起的,则显示您想要的任何内容。

      但是,如果您正在学习 PHP 编程,则不应使用 mysql_,因为它已被弃用 - 使用 mysqli 或 PDO

      【讨论】:

        【解决方案4】:
         $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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-23
          • 1970-01-01
          • 2022-01-20
          • 2012-03-06
          • 2015-03-24
          • 1970-01-01
          • 1970-01-01
          • 2021-01-23
          相关资源
          最近更新 更多