[英]Check if record already exists before creating a new record
我有以下代码,它通过保存在表单中输入的内容从表单发布到数据库。 这可行,但是我想知道在添加为重复和纠正错误之前如何检查输入的记录是否已经存在。 在这种情况下,应该检查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... ";
}
}
尝试这个
<?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... ";
}
}
}
?>
这个问题已经在stackoverflow中问过了,您可以在这里查看此逻辑,用于已经存在的记录检查,但是仅在更新表单值并使用条件逻辑的情况下:检查记录是否存在; 如果有,请更新它;如果没有,请创建它 。 这些上面的链接将帮助您获得答案。 我想这会帮助你
如果学生ID字段是UNIQUE索引,则尝试添加相同的ID将失败。
然后,您可以捕获该错误,如果是由于现有记录所致,则显示所需的内容。
但是,如果您正在学习PHP编程,则不应使用mysql_,因为它已过时-使用mysqli或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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.