输入时输入数据库的重复值

时间:2017-03-26 08:13:32

标签: php mysql

我是网络开发的新手,并设法创建一个收集数据的表单,但发现有一些重复的值设法在提交时进入数据库。关于如何维护数据的任何想法只被捕获一次到数据库中?以下是我的代码,用于在提交时发布表单的详细信息。

<?php include 'database.php';?>
<?php

$customer_name = data_input($_POST["customer_name"]);
$customer_gender = data_input($_POST["customer_gender"]);
$mobile_number = data_input($_POST["mobile_number"]);
$unique_box_id = data_input($_POST["unique_box_id"]);
$casn_number = data_input($_POST["casn_number"]);
$customer_address = data_input($_POST["customer_address"]);
$dso_region = data_input($_POST["dso_regions"]);
$state = data_input($_POST["state"]);
$decoder_type = data_input($_POST["decoder_type"]);
$antennae_type = data_input($_POST["antennae_type"]);
$brand_of_box = data_input($_POST["brand_of_box"]);
$call_category = data_input($_POST["call_category"]);
$complaint_category = data_input($_POST["complaint_category"]);
$agent_notes = data_input($_POST["agent_notes"]);
$resolution = data_input($_POST["resolution"]);
$agent_name = data_input($_POST["agent_name"]);
function data_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

mysqli_query($connect,"INSERT INTO customer_details (NAMES, GENDER,MOBILE_NUMBER,UNIQUE_BOX_ID,CASN_NUMBER,
        CUSTOMER_ADDRESS,DSO_REGION,STATE,DECODER_TYPE,ANTENNAE_TYPE,BRAND_OF_BOX,
        CALL_CATEGORY,COMPLAINT_CATEGORY,AGENT_NOTES,RESOLUTION,AGENT_NAME)
        VALUES ('$customer_name', '$customer_gender', '$mobile_number','$unique_box_id','$casn_number','$customer_address','$dso_region','$state','$decoder_type',
        '$antennae_type','$brand_of_box','$call_category','$complaint_category','$agent_notes','$resolution','$agent_name')");

if (mysqli_affected_rows($connect)> 0){
    echo "<p>Customer Details submitted</p>";
    //echo "<a href="PHPcrm.php">Go Back</a>";
    header("Location: {$_SERVER['HTTP_REFERER']}");
    exit;
    } else {
        echo "Customer Details NOT submitted<br />";
        echo mysqli_error ($connect);
    }

    mysql_close($connnet)

?>

1 个答案:

答案 0 :(得分:0)

我认为您的if条件错误,您正在检查连接变量 但是你应该检查另一个变量,比如这个

$insertQuert = mysqli_query($connect,"INSERT INTO ... 

然后在条件

if (mysqli_affected_rows($insertQuery)> 0){