gpt4 book ai didi

由于 if 语句,PHP 表单无法验证或发送?

转载 作者:行者123 更新时间:2023-11-30 00:31:09 25 4
gpt4 key购买 nike

我正在尝试通过让用户填写已验证的表单来更新页面上的表格。由于某种原因,当我添加验证 if 语句时,除了当用户单击“添加新”时刷新之外,表单不会执行任何操作。如果我删除 if 语句,表就会更新。我缺少什么?

<?php
session_start();
require_once "db.php";

if ( isset ($_POST['url']) && isset ($_POST['email'])
&& isset ($_POST['length']) && isset ($_POST['rating']) ) {

//Reject empty form fields
if (strlen($_POST['url']) < 1 || strlen($_POST['email']) <1 || strlen ($_POST['length']) <1 || strlen($_POST['rating']) < 1) {
$_SESSION['error'] = 'All values are required';
header('Location: add.php');
return;
}

//Reject urls that are not http or https
$url = $_POST['url'];
if (strpos($url,"http://") !== 0 && strpos($url, "https://") !== 0 ) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}

//Reject emails without '@' symbol
if ( strpos($_POST['email'], "@") === false ) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}

//Reject track lengths or rating numbers that are too small
if ( $_POST['length'] +0 < 1 || $_POST['rating'] +0 < 1 ) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}

$sql = "INSERT INTO videos (url, email, length, rating)
VALUES (:url, :email, :length, :rating)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':url' => $_POST ['url'],
':email' => $_POST ['email'],
':length' => $_POST['length'],
':rating' => $_POST['rating']));
$_SESSION['success'] = 'Record Added';
header( 'Location: index.php' ) ;
return;

}

?>

<p>Add A New Video</p>
<form method="post">
<p>URL:
<input type="text" name="url"></p>
<p>Email:
<input type="text" name="email"></p>
<p>Length:
<input type="text" name="length"></p>
<p>Rating:
<input type="text" name="rating"></p>
<p><input type="submit" value="Add New"/>
<a href="index.php">Cancel</a></p>
</form>

最佳答案

我想说,使用 strpos 进行验证很容易出现问题。尝试使用这样的正则表达式:

if (! preg_match('/[^@\s]{3,}@([-a-z0-9]{2,}\.)+[a-z]{2,}/', $_POST['e_mail'])) { $_SESSION['error'] = 'Error in input data'; }

关于由于 if 语句,PHP 表单无法验证或发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507729/

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