PHP 检查输入中的错误,如果没有错误则执行并上传 [英] PHP check for errors in input, if no errors execute and upload

查看:20
本文介绍了PHP 检查输入中的错误,如果没有错误则执行并上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究注册系统,但遇到了一些问题.
我先粘贴一个简化版的代码:

I'm currently working on a registration system and ran into some problem.
I'll start with pasting a simplified version of the code before:

session_start();

if (isset($_SESSION['logged_in'])) {

header('Location: #notLoggedIn');
exit;
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if //if field is empty {
            //display error
        } else if //check if any unallowed characters {
            //display another error
        } else {
            //give the checked input a string/variable, ex: $name= ($_POST["name"]);
        }

        // Like 4-5 other checks running in the same way as above

    }

    $query = $pdo->prepare('INSERT INTO table (a, b, c, d, e) VALUES (?,?,?,?,?)');

    $query->bindValue(1, $1);
    $query->bindValue(2, $2);
    $query->bindValue(3, $3);
    $query->bindValue(4, $4);
    $query->bindValue(5, $5);

    $query->execute();

    header('Location: index.php');
    exit;
    }

问题在于它一次运行所有内容,只是将我重定向到 index.php.
我如何确保在运行之前首先检查表单是否已提交.
之后我希望它检查所有字段中的任何错误.如果有错误,请停止.
但如果没有错误,就继续上传到我的数据库.

The problem is the fact that it runs everything at once and just redirects me to index.php.
How do I make sure it first of all checks if the form has been submitted before running.
After that I want it to check for any errors in ALL fields. If there are errors, stop.
But if there are no errors, just continue on and upload to my database.

我确实认为我在一条好路上,但目前很卡,任何帮助或朝正确方向推动都会很棒!
谢谢!

I do think that I'm on a goodway, but currently pretty stuck, any help or push in the correct direction would be awesome!
Thank you!

推荐答案

// error = 0  means no error found you can continue to upload...
if ($_FILES['file']['error'] == 0) {

}

以下是解释的所有错误:http://php.net/manual/en/features.file-upload.errors.php

Here are all of the errors explained: http://php.net/manual/en/features.file-upload.errors.php

UPLOAD_ERR_OK 值:0;没有错误,文件上传成功.

UPLOAD_ERR_OK Value: 0; There is no error, the file uploaded with success.

UPLOAD_ERR_INI_SIZE 值:1;上传的文件超过php.ini 中的 upload_max_filesize 指令.

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE 值:2;上传的文件超过在 HTML 表单中指定的 MAX_FILE_SIZE 指令.

UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL 值:3;上传的文件只是部分上传.

UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE 值:4;没有上传文件.

UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR 值:6;缺少临时文件夹.在 PHP 5.0.3 中引入.

UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE 值:7;无法将文件写入磁盘.在 PHP 5.1.0 中引入.

UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION 值:8;PHP 扩展停止了文件上传.PHP 没有提供确定哪个扩展导致的方法文件上传停止;检查加载的扩展列表phpinfo() 可能会有所帮助.在 PHP 5.2.0 中引入.

UPLOAD_ERR_EXTENSION Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.

验证输入字段

if(empty($_POST['name'])&&empty($_POST['password'])){
 //fields empty show error here
}else if (is_numeric($username[0])){
    echo 'First character must be a letter';
}
else if (!preg_match('/^[a-zA-Z0-9]+$/', $username)) {
     echo 'Only letters and numbers are allowed';
}else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
     echo 'Invalid email address.'; 
}else if(!preg_match("/^[\pL\s,.'-]+$/u", $name)) {
     echo 'Invalid name.'; 
}

这篇关于PHP 检查输入中的错误,如果没有错误则执行并上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆