gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:53:36 26 4
gpt4 key购买 nike

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

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

我尝试比较字符串,但它仅在有 2 个变量时才有效,我也尝试使用 in_array() 但这不起作用。我发布这两个页面是为了如果有人能指出我正确的方向,我会很高兴并继续我的项目。

在允许将 q1q1 插入数据库之前,我想确保 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';
?>

最佳答案

你可以这样做:

$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 执行类似的方法来在客户端执行此操作。

它所做的就是循环 post 数组的键,然后检查其他项目的值,跳过与之比较的项目。这意味着您将获得每个重复项的结果,但我假设您希望在所有匹配项上添加警告?

无论如何,希望它能帮助您更接近解决方案。

关于php - 使用 $_POST 验证 html/PHP 字段中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43349234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com