在我的Android应用程序中,我使用JSON将详细信息保存到服务器,并使用PHP文件。在我的PHP代码中,我想检查重复的条目,现在在我的代码重复条目不检查。我在下面给出了我的代码,其中enties是用户的名字,中间名,姓氏,出生日期,出生月份,出生年份,电子邮件,手机号码和备用电子邮件。我需要检查重复的条目......比如,如果姓氏,姓氏和出生年份相同,则无需保存。
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['firstname']) && isset($_POST['middlename']) && isset($_POST['lastname']) && isset($_POST['dayb']) && isset($_POST['daym']) && isset($_POST['dayy']) && isset($_POST['mnumber']) && isset($_POST['pemail']) && isset($_POST['aemail'])) {
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dayb = $_POST['dayb'];
$daym = $_POST['daym'];
$dayy = $_POST['dayy'];
$mnumber = $_POST['mnumber'];
$pemail = $_POST['pemail'];
$aemail = $_POST['aemail'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO userentry(firstname,middlename, lastname, dayb, daym, dayy, mnumber, pemail, aemail) VALUES('$firstname', '$middlename', '$lastname', '$dayb', '$daym', '$dayy', '$mnumber', '$pemail', '$aemail')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
else
{
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
答案 0 :(得分:0)
我不懂PHP语法,但你只需要更改插入查询
实施例: -
INSERT INTO userentry (firstname,middlename, lastname, dayb, daym, dayy, mnumber, pemail, aemail) SELECT '$firstname', '$middlename', '$lastname', '$dayb', '$daym', '$dayy', '$mnumber', '$pemail', '$aemail' WHERE NOT EXISTS (SELECT aemail,mnumber FROM tbl_responce WHERE aemail = '$aemail' and mnumber ='$mnumber')
答案 1 :(得分:0)
我认为您需要在插入数据库之前进行检查..然后尝试使用此查询。
SELECT * FROM your_table WHERE first_name = $_POST['firstname'] AND last_name = $_POST['firstname']
然后计算所选查询的数量。
if($result) {
//there is a duplicate data in database. Do not insert
}
else{
//insert your data here.
}
答案 2 :(得分:0)
下面是适合你的代码
$resp = mysql_fetch_assoc(mysql_query("SELECT count(*) as is_exist
FROM userentry
WHERE firstname='".$firstname."' and lastname='".$lastname."'
and dayb='".$dayb."'" and daym='".$daym."'"
and dayy='".$dayy."'"));
if($resp['is_exist']==0){
// Insert code
$result = mysql_query("INSERT INTO userentry(firstname,middlename, lastname, dayb, daym, dayy, mnumber, pemail, aemail) VALUES('$firstname', '$middlename', '$lastname', '$dayb', '$daym', '$dayy', '$mnumber', '$pemail', '$aemail')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}else
{
//prompt that the record exist!
$response["success"] = 0;
$response["message"] = "Oops! User Already Exist";
// echoing JSON response
echo json_encode($response);
}