我有以下代码,通过保存表单中输入的内容,从表单发布到数据库。这有效,但我想知道我怎么能够检查我输入的记录是否已经存在,然后将其添加为dublicate并且错误。在这种情况下,应检查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... ";
}
}
答案 0 :(得分:1)
试试这个
<?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... ";
}
}
}
?>
答案 1 :(得分:1)
此问题已在stackoverflow中提出,您可以在Logic for already exist record check but only in case of updated form values和using conditional logic : check if record exists; if it does, update it, if not, create it查看此问题。 以上链接将帮助您获得答案。 我想这会对你有帮助
答案 2 :(得分:0)
如果student id字段是UNIQUE索引,则尝试添加相同的id将失败。
然后您可以捕获错误,如果它是由现有记录引起的,则显示您想要的任何内容。
但是如果你正在学习PHP编程,你不应该使用mysql_,因为它已被弃用 - 要么使用mysqli,要么使用PDO
答案 3 :(得分:0)
$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
}