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

时间:2016-04-26 10:16:17

标签: php mysql pdo

下面是我试图验证表中是否存在相同名称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来完成循环以检查每个字段是否包含相同的名称。

3 个答案:

答案 0 :(得分:0)

您可以尝试: -

$name = ($_POST['name']) ? $name = trim($_POST['name']) : $name='';

答案 1 :(得分:0)

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中检查名称/经理/高级中的双打:

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!');
    }
}

答案 2 :(得分:0)

您需要从

更新以下查询行
$sql = "SELECT COUNT(*) AS num FROM users";
$stmt = $pdo->prepare($sql);

到这个

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