使用$ _POST验证html / PHP字段中的重复条目

时间:2017-04-11 14:45:21

标签: php html mysql validation

paper_add.php是一个html表单,用于将值发布到另一个名为paper_entry.php的php页面。

我想使用验证不允许重复条目进入数据库,而是会给出错误消息,表明某些值是重复的。

我尝试比较字符串,但只有在有2个变量的情况下才有效,我也尝试使用in_array(),但这不起作用。我发布这两个页面,以便如果有人能指出我正确的方向,我会非常高兴并继续我的项目。

我想确保q1q1与q1q2不同,依此类推之后才允许将其插入数据库

<?php include 'header.php'; ?>
<div class="container">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th colspan='2'><u>Add Paper</u></th>
            </tr>
        </thead>
        <tbody>
            <form method="post" action="paper_entry.php">
            <tr>
                <td>Examination </td>
                <td><input type="text" required="required" class="form-control"   name="examination"></td>
            </tr>
            <tr>
                <td>Subject </td>
                <td><input type="text" required="required" class="form-control"   name="subject"></td>
            </tr>
            <tr>
                <td>Subject Code </td>
                <td><input type="text" required="required" class="form-control"  name="subject_code"></td>
            </tr>
            <tr>
                <td>Category</td>
                <td><input type="text" required="required" class="form-control"  name="category"></td>
            </tr>
            <tr>
                <td>Month & Year:</td>
                <td><input type="text" required="required" class="form-control"  name="month_year"></td>
            </tr>
            <input type="hidden" required="required" class="form-control"  name="created_by" value="<?php echo $_SESSION ['user'] ?>">
            <tr>
                <td>Question 1 (1) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q1"></td>
            </tr>
            <tr>
                <td>Question 1 (2) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q2"></td>
            </tr>
            <tr>
                <td>Question 1 (3) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q3"></td>
            </tr>
            <tr>
                <td>Question 1 (4) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q4"></td>
            </tr>
            <tr>
                <td>Question 1 (5) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q5"></td>
            </tr>
            <tr>
                <td>Question 1 (6) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q6"></td>
            </tr>
            <tr>
                <td>Question 1 (7) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q7"></td>
            </tr>
            <tr>
                <td>Question 1 (8) :</td>
                <td><input type="text" required="required" class="form-control"  name="q1q8"></td>
            </tr>
            <tr>
                <td>Question 2 (A) :</td>
                <td><input type="text" required="required" class="form-control"  name="q2qa"></td>
            </tr>
            <tr>
                <td>Question 2 (B) :</td>
                <td><input type="text" required="required" class="form-control"  name="q2qb"></td>
            </tr>
            <tr>
                <td>Question 2 (Or A) :</td>
                <td><input type="text" required="required" class="form-control"  name="q2qoa"></td>
            </tr>
            <tr>
                <td>Question 2 (Or B) :</td>
                <td><input type="text" required="required" class="form-control"  name="q2qob"></td>
            </tr>
            <tr>
                <td>Question 3 (A) :</td>
                <td><input type="text" required="required" class="form-control"  name="q3qa"></td>
            </tr>
            <tr>
                <td>Question 3 (B) :</td>
                <td><input type="text" required="required" class="form-control"  name="q3qb"></td>
            </tr>
            <tr>
                <td>Question 3 (Or B) :</td>
                <td><input type="text" required="required" class="form-control"  name="q3qob"></td>
            </tr>
            <tr>
                <td>Question 4 (A) :</td>
                <td><input type="text" required="required" class="form-control"  name="q4qa"></td>
            </tr>
            <tr>
                <td>Question 4 (B) :</td>
                <td><input type="text" required="required" class="form-control"  name="q4qb"></td>
            </tr>
            <tr>
                <td>Question 5 (1) :</td>
                <td><input type="text" required="required" class="form-control"  name="q5q1"></td>
            </tr>
            <tr>
                <td>Question 5 (2) :</td>
                <td><input type="text" required="required" class="form-control"  name="q5q2"></td>
            </tr>
            <tr>
                <td>Question 5 (3) :</td>
                <td><input type="text" required="required" class="form-control"  name="q5q3"></td>
            </tr>
            <tr>
                <td>Submit : </td>
                <td><input type="submit" name="submit"></td>
            </tr>
        </tbody>
    </table>
</div>

paper_entry.php

<?php
include 'header1.php';
$examination  = $_POST["examination"];
$subject      = $_POST["subject"];
$subject_code = $_POST["subject_code"];
$category     = $_POST["category"];
$month_year   = $_POST["month_year"];
$created_by   = $_POST["created_by"];

$q1q1  = $_POST["q1q1"];
$q1q2  = $_POST["q1q2"];
$q1q3  = $_POST["q1q3"];
$q1q4  = $_POST["q1q4"];
$q1q5  = $_POST["q1q5"];
$q1q6  = $_POST["q1q6"];
$q1q7  = $_POST["q1q7"];
$q1q8  = $_POST["q1q8"];
$q2qa  = $_POST["q2qa"];
$q2qb  = $_POST["q2qb"];
$q2qoa = $_POST["q2qoa"];
$q2qob = $_POST["q2qob"];
$q3qa  = $_POST["q3qa"];
$q3qb  = $_POST["q3qb"];
$q3qob = $_POST["q3qob"];
$q4qa  = $_POST["q4qa"];
$q4qb  = $_POST["q4qb"];
$q5q1  = $_POST["q5q1"];
$q5q2  = $_POST["q5q2"];
$q5q3  = $_POST["q5q3"];

include 'dbconnect.php';

$sql = "INSERT INTO paper (id , examination, subject,subject_code,category,month_year,created_by,q1q1,q1q2,q1q3,q1q4,q1q5,q1q6,q1q7,q1q8,q2qa,q2qb,q2qoa,q2qob,q3qa,q3qb,q3qob,q4qa,q4qb,q5q1,q5q2,q5q3)
          VALUES ('','$examination','$subject','$subject_code','$category','$month_year','$created_by','$q1q1','$q1q2','$q1q3','$q1q4','$q1q5','$q1q6','$q1q7','$q1q8','$q2qa','$q2qb','$q2qoa','$q2qob','$q3qa','$q3qb','$q3qob','$q4qa','$q4qb','$q5q1','$q5q2','$q5q3')";

if (mysqli_query($conn, $sql)) {
    echo "<center> <h2><a href='paper_list.php'> Paper Added, Click Here For List. </h2> </center></a>";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

include 'footer.php';
?>

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

$post_array = [
        'q1q1' => 'test ', 
        'q1q2' => 'testing', 
        'q1q3' => 'test', 
        'q2qa' => 'tester', 
        'q2qoa' => 'test2', 
        'q3qob' => 'tests'
    ];

$keys = array_keys($post_array);

foreach ($keys as $key) {
    foreach ($post_array as $k => $v) {
        if ($key != $k) {
            if (trim(strtolower($v)) == trim(strtolower($post_array[$key]))) {
                echo "The value '{$v}' in {$key} matches the value in {$k}<br />";
            }
        } else {
            continue;
        }
    }
}

我有一种直觉感觉array_reduce会更好用,但这应该有效,你需要修改它以适应你的帖子数组,我在这里只是添加了默认值作为例子。你也可以用Javascript做类似的方法来做客户端。

所有这一切都是循环你的帖子数组的键,然后检查其他项目的值,跳过它与之比较的项目。这意味着您将获得每个副本的结果,但我认为您想要在所有匹配项上添加警告?

无论如何,希望有助于让您更接近解决方案。