gpt4 book ai didi

php - 如何使用 PHP 验证表中的所有行

转载 作者:行者123 更新时间:2023-11-29 21:05:23 25 4
gpt4 key购买 nike

下面是我尝试验证表中是否存在相同名称、idemail 的脚本,问题是即使我添加每个字段的新输入我仍然得到“数据已存在”,如何解决此问题,以及如何使用每个字段的正则表达式进行验证。对于 id 我需要使用正则表达式三个字母和 3 个数字。

if(isset($_POST['register'])){

//Retrieve the field values from our registration form.
$name = !empty($_POST['name']) ? trim($_POST['name']) : null;
$ccode = !empty($_POST['id']) ? trim($_POST['id']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;

$sql = "SELECT COUNT(*) AS num FROM users";
$stmt = $pdo->prepare($sql);

$stmt->bindValue(':name', $name);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':email', $email);

$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['num'] > 0){
die('The Data already exists!');
} else {
$sql = "INSERT INTO users (name, id, email) VALUES (:name, :id, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':email', $email);
$result = $stmt->execute();
if ($result) {
echo 'User aadded successfully!';
}
}
}
<form action="sign.php" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" required><br>
<label for="id">id</label>
<input type="text" id="" name="id" required><br>
<label for="email">Email</label>
<input type="email" id="email" name="email" required><br>
<input type="submit" name="register" value=" Click to Add"></button>
</form>

以及如何验证字段是否具有相同的名称。例如,我有 3 个输入字段,在第一个字段中输入一个人的姓名,在第二个或第三个字段中,如果我输入相同的姓名,则当我单击提交按钮时应该显示错误,可以使用 for 来完成吗?循环检查每个字段是否包含相同的名称。

最佳答案

uuhm,您对这些变量是否已存在的行的检查没有使用任何变量?

$sql = "SELECT COUNT(*) AS num FROM users"; // Where are the variables?
$stmt = $pdo->prepare($sql);


$stmt->bindValue(':name', $name);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':email', $email);

$stmt->execute();

您想要做的是:

$sql = "SELECT COUNT(*) AS num FROM users WHERE name = :name OR id = :id OR email = :email"; // Where are the variables?
$stmt = $pdo->prepare($sql);


$stmt->bindParam(':name', $name);
$stmt->bindParam(':id', $id);
$stmt->bindParam(':email', $email);

$stmt->execute();

我对使用 PDO 不太有经验。我自己更喜欢MySQLi

您应该阅读有关 prepared statements 的更多信息

编辑:

基于注释的新查询,只需在 PHP 中检查 name/manager/senior 中的 double 即可:

if($name != $manager && $manager != $senior && $name != $senior) {
$sql = "SELECT COUNT(*) AS num FROM users WHERE (name = :name OR manager = :manager OR senior = :senior)";
$stmt = $pdo->prepare($sql);


$stmt->bindParam(':name', $name);
$stmt->bindParam(':manager', $manager);
$stmt->bindParam(':senior', $senior);

$stmt->execute();
// insert other stuff
} else {
// insert error here
}

完整代码:

if(isset($_POST['register'])){

//Retrieve the field values from our registration form.
$name = !empty($_POST['name']) ? trim($_POST['name']) : null;
$manager= !empty($_POST['manager']) ? trim($_POST['manager']) : null;
$senior= !empty($_POST['senior']) ? trim($_POST['senior']) : null;

if($name != $manager && $manager != $senior && $name != $senior) {
$sql = "SELECT COUNT(*) AS num FROM users WHERE (name = :name OR manager = :manager OR senior = :senior)";
$stmt = $pdo->prepare($sql);


$stmt->bindParam(':name', $name);
$stmt->bindParam(':manager', $manager);
$stmt->bindParam(':senior', $senior);

$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['num'] > 0){
die('The Data already exists!');
} else {
$sql = "INSERT INTO users (name, manager, senior) VALUES (:name, :manager, :senior)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':manager', $manager);
$stmt->bindParam(':senior', $senior);
$result = $stmt->execute();
if ($result) {
echo 'User added successfully!';
}
}
} else {
die('Duplicate in name/manager/senior!');
}
}

关于php - 如何使用 PHP 验证表中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861967/

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