为什么我的功能不做任何事情?

时间:2016-01-13 19:38:45

标签: php

我想要发生的事情:我想使用一个函数来检查错误并防止这些错误被放入我的数据库中。

详细信息(以及当前正在发生的事情):我有一个检查我定义的错误的函数。如果发现错误,则将其放在数组中。找到所有错误后,我退出我的功能。然后我放置我想要的名称(来自CSV格式的表格),然后将它们上传到我的数据库。但是,在第一次迭代之后,即使在我的函数中找到了错误检查,仍会将带有错误的名称放入数据库中。如何阻止那些有错误的名字被放入我的数据库?

另请注意,在我的HTML / PHP表单中,我确实启动了会话,并且我也取消了会话。

我的流程代码:

session_start();

$formInputNames = $_POST['names'];

$active = (isset($_POST['activate'])) ? $_POST['activate'] : false;
//checks if activate checkbox is being used
$email = '@grabby.com';
echo "<br>";
echo "<br>";

$fullnames = explode(", ", $_POST['names']);

if ($active == true) {
    $active = '1';
    //sets activate checkbox to '1' if it has been selected
}
/*----------------------Function to Insert User---------------------------*/
         A Function is here to place names and other fields in database.
/*-------------------------End Function to Insert User--------------------*/

/*-----------------------Function for Errors---------------------*/

function errorCheck($fullname,$nameSplit,$formInputNames){
    if ($formInputNames == empty($fullname)){
            $_SESSION['error'][] = '<br><br> Error: Name Missing Here: '.$fullname.'<br><br>';
        }
        elseif ($formInputNames == empty($nameSplit[0])) {
            $_SESSION['error'][] = '<br><br> Error: First Name Missing Here: '.$fullname.'<br><br>';
        }
        elseif ($formInputNames == empty($nameSplit[1])) {
            $_SESSION['error'][] = '<br><br> Error: Last Name Missing Here: '.$fullname.'<br><br>';
        }
        elseif (preg_match('/[^A-Za-z, ]/', $fullname)) {
            $_SESSION['error'][] = '<br><br> Error: Illegal Character Found in: '.$fullname.'<br><br>';
        }
}

/*-----------------------------End Function for Errors------------------------*/

/*--------------------------Function for Redirect-------------------------*/

function redirect($url){
    $string = '<script type="text/javascript">';
    $string .= 'window.location = "' .$url. '"';
    $string .= '</script>';

    echo $string;
}

/*-------------------------End Function for Redirect-----------------------*/

//  Connect to database
     I connect to the database here

foreach ($fullnames as $fullname) {
    $nameSplit = explode(" ", $fullname);

errorCheck($fullname,$nameSplit,$formInputNames);

            //opens the database
I Open the database here

        $firstName = $nameSplit[0];//sets first part of name to first name
        $lastName = $nameSplit[1];//sets second part of name to last name
        $emailUser = $nameSplit[0].$email;//sets first part and adds email extension

        newUser($firstName,$lastName,$emailUser,$active,$conn);
}//ends fullnames foreach

if (count($_SESSION['error']) == 0) {
    redirect('viewAll.php');
} else {
    redirect('form.php');
}

我希望我清楚自己想要实现的目标,但如果我不让我知道的话。谷歌,这个网站和其他人没有以最好的方式帮助我。先谢谢!

编辑:我的意思是'我的功能没有做任何事'

我希望我的函数能够阻止输入数据库的错误名称。我试图通过测试我在函数中输入的名称并防止它们进入来实现这一点。现在发生的事情(以及正在发生的事情)是,产生错误并在出现错误时显示消息。

如果这没有帮助,请以这种方式看待它。

if (empty($fullname)){
            $_SESSION['error'][] = '<br><br> Error: Name Missing Here: '.$fullname.'<br><br>';
        } elseif (empty($nameSplit[0])) {
            $_SESSION['error'][] = '<br><br> Error: First Name Missing Here: '.$fullname.'<br><br>';
        } elseif (empty($nameSplit[1])) {
            $_SESSION['error'][] = '<br><br> Error: Last Name Missing Here: '.$fullname.'<br><br>';
        } elseif (preg_match('/[^A-Za-z, ]/', $fullname)) {
            $_SESSION['error'][] = '<br><br> Error: Illegal Character Found in: '.$fullname.'<br><br>';
        } else {
            $firstName = $nameSplit[0];//sets first part of name to first name
            $lastName = $nameSplit[1];//sets second part of name to last name
            $emailUser = $nameSplit[0].$email;//sets first part and adds email extension
    newUser($firstName,$lastName,$emailUser,$active,$conn);
       }

以上(以及我的其余代码)将完成我想要实现的目标。

基本上我正在努力实现的目标,如何在我的函数中使用该finial else语句。

2 个答案:

答案 0 :(得分:1)

我将原始代码的部分内容与更新中的大部分代码组合在一起。基本的想法是你将errorCheck()返回一个布尔值。检查该布尔值以查看是否应继续并将记录插入数据库。

session_start();

$formInputNames = $_POST['names'];

$active = (isset($_POST['activate'])) ? $_POST['activate'] : false;
//checks if activate checkbox is being used
$email = '@grabby.com';
echo "<br>";
echo "<br>";

$fullnames = explode(", ", $_POST['names']);

if ($active == true) {
    $active = '1';
    //sets activate checkbox to '1' if it has been selected
}
/*----------------------Function to Insert User---------------------------*/
         //A Function is here to place names and other fields in database.
/*-------------------------End Function to Insert User--------------------*/

/*-----------------------Function for Errors---------------------*/

function errorCheck($fullname,$nameSplit,$formInputNames){
    $isValid = false;

    if (empty($fullname)){
        $_SESSION['error'][] = '<br><br> Error: Name Missing Here: '.$fullname.'<br><br>';
    } elseif (empty($nameSplit[0])) {
        $_SESSION['error'][] = '<br><br> Error: First Name Missing Here: '.$fullname.'<br><br>';
    } elseif (empty($nameSplit[1])) {
        $_SESSION['error'][] = '<br><br> Error: Last Name Missing Here: '.$fullname.'<br><br>';
    } elseif (preg_match('/[^A-Za-z, ]/', $fullname)) {
        $_SESSION['error'][] = '<br><br> Error: Illegal Character Found in: '.$fullname.'<br><br>';
    } else {
        $isValid = true;
    }

    return $isValid;
}

/*-----------------------------End Function for Errors------------------------*/

/*--------------------------Function for Redirect-------------------------*/

function redirect($url){
    $string = '<script type="text/javascript">';
    $string .= 'window.location = "' .$url. '"';
    $string .= '</script>';

    echo $string;
}

/*-------------------------End Function for Redirect-----------------------*/

//  Connect to database
     //I connect to the database here

foreach ($fullnames as $fullname) {
    $nameSplit = explode(" ", $fullname);

    if(errorCheck($fullname,$nameSplit,$formInputNames))
    {
        //opens the database
        //I Open the database here

        $firstName = $nameSplit[0];//sets first part of name to first name
        $lastName = $nameSplit[1];//sets second part of name to last name
        $emailUser = $nameSplit[0].$email;//sets first part and adds email extension

        newUser($firstName,$lastName,$emailUser,$active,$conn);
    }
}//ends fullnames foreach

if (count($_SESSION['error']) == 0) {
    redirect('viewAll.php');
} else {
    redirect('form.php');
}

答案 1 :(得分:0)

$formInputNames == empty($fullname)
$formInputNames == empty($nameSplit[0])
$formInputNames == empty($nameSplit[1])

empty会返回boolean值,但您正在检查它是否等于$formInputNames

我会尝试改进您的代码:

function errorCheck($fullname,$nameSplit,$formInputNames) {
    if (empty($formInputNames) && empty($fullname)) {
        $_SESSION['error'][] = '<br><br> Error: Name Missing Here: '.$fullname.'<br><br>';
    } elseif (empty($formInputNames) && empty($nameSplit[0])) {
        $_SESSION['error'][] = '<br><br> Error: First Name Missing Here: '.$fullname.'<br><br>';
    } elseif (empty($formInputNames) && empty($nameSplit[1])) {
        $_SESSION['error'][] = '<br><br> Error: Last Name Missing Here: '.$fullname.'<br><br>';
    } elseif (preg_match('/[^A-Za-z, ]/', $fullname)) {
        $_SESSION['error'][] = '<br><br> Error: Illegal Character Found in: '.$fullname.'<br><br>';
    }
}

我使用&&代替||,因为我真的不知道你在这里想要达到的目标。