我的PHP中有一个未定义的变量

时间:2017-07-14 04:10:42

标签: php

我是PHP的新手,并且一直试图与mysqli集成。显然在我的代码的第19行,显示的变量是未定义的,但据我所知,我定义了它。

这是代码。我环顾四周,但我找不到能隔离它的东西。

<?php

include("connect.php");

$error = "";

if(isset($_POST['submit']))
    {
    $characterName = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordConfirm = $_POST['passwordConfirm'];

    $image = $_FILES['image']['name'];
    $tmp_image = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];
    }

    if(strlen($fname) < 3)
    {
        $error = "Character name is too short";
    }


    else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $error = "Please enter a valid email address";
    }

    else if(strlen($password) < 8)
    {
        $error = "Password must be more than 8 characters";
    }

    else if($password === $passwordConfirm)
    {
        $error = "Password does not match";
    }

    else if($image = "")
    {
        $error = "Please upload an Avatar";
    }

    else
    {
        $error = "You have successfully registered";
    }


  ?>

表单代码:

<form method="post" action="index.php" enctype="multipart/form-data">
            <label>Character Name:</label><br />
            <input type="text" name="fname" /><br /><br />
            <label>Email:</label><br />
            <input type="text" name="email" /> <br /><br />
            <label>Password:</label><br />
            <input type="password" name="password" /><br /><br />
            <label>Reenter Password:</label><br />
            <input type="password" name="passwordConfirm" /><br /><br />
            <label>Send us an Avatar:</label><br />
            <input type="file" name="image" /><br /><br />
            <input type="submit" name="submit" value="submit" />
        </form>

2 个答案:

答案 0 :(得分:4)

if(strlen($fname) < 3) {
   $error = "Character name is too short";
}

这里有错误,$ fname未定义。你有什么意思是$ _POST ['fname'] ;.您存储在$ characterName中的所以将其更改为:

if(strlen($characterName) < 3){
   $error = "Character name is too short";
}

无论如何,因为你只在isset($ _ POST ['submit'])中定义你的变量,如果没有设置,下面的行将会失败。这是一个如何工作的例子。 $ _POST ['submit']只有在用post参数(formular,ajax ..)调用它时才会被定义,所以如果你直接打开php文件就行不通。我添加了一些评论来说清楚。

<?php

include("connect.php");

$error = "";

if(isset($_POST['submit'])) {
    //If this block of variable declaration failed it wouldn´t define the variables
    $characterName = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordConfirm = $_POST['passwordConfirm'];

    $image = $_FILES['image']['name'];
    $tmp_image = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];

    //So we led Php only check the variables if a submit is provided
    if(strlen($characterName) < 3) {

        $error = "Character name is too short";

    } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {

        $error = "Please enter a valid email address";

    } else if(strlen($password) < 8) {

        $error = "Password must be more than 8 characters";

    } else if($password === $passwordConfirm) {   

        $error = "Password does not match";

    } else if($image = "") {

        $error = "Please upload an Avatar";

    } else {

        $error = "You have successfully registered";

    }
} else {
    //If there is no submit we land here
    $error = "No data provided";
}




  ?>

答案 1 :(得分:0)

如果未发布提交,则只会收到未定义的变量错误。为了避免那些错误。你只需改变你的代码

if(isset($_POST['submit']))
    {
    $characterName = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordConfirm = $_POST['passwordConfirm'];

    $image = $_FILES['image']['name'];
    $tmp_image = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];
    }
else
{
 $error ="Your submit is not posted.";
 exit(); //Without it would again trigger the undefined variables. 
}