为什么INSERT INTO无法起作用? PDO

时间:2019-12-25 17:28:55

标签: php mysql function pdo insert

我正在尝试构建一个函数以将新用户插入数据库。当我尝试插入不带函数的用户时,它可以工作,但是当我将代码带入函数中时,为什么不起作用?我什至没有收到错误消息。仅出现空白页。

我的功能

function add_new_user($user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass) {

    try {

            $sql = "INSERT INTO users (user_role, user_firstname, user_lastname, user_email, user_pnumber, user_project, user_location, user_name, user_pass) VALUES (?,?,?,?,?,?,?,?,?)";
            $insert = $conn->prepare($sql);
            $insert->execute([$user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass]);

        } 

        catch( PDOEXception $e ) {

           echo $e->getMessage(); // display error
           exit();

       }

}

此代码无效。

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $user_role          = secured_post("user-role");
    $user_firstname     = secured_post("user-firstname");
    $user_lastname      = secured_post("user-lastname");
    $user_email         = secured_post("user-email");
    $user_pnumber       = secured_post("user-pnumber");
    $user_project       = secured_post("user-project");
    $user_location      = secured_post("user-location");
    $user_name          = secured_post("user-name");
    $user_pass          = secured_post("user-pass");

    if (multi_empty($user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass)) {

        add_new_user($user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass);


   } else {

        echo "Sorry.";

   }

}

但是此代码有效。为什么我不能使用“ add_new_user”功能?

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $user_role          = secured_post("user-role");
    $user_firstname     = secured_post("user-firstname");
    $user_lastname      = secured_post("user-lastname");
    $user_email         = secured_post("user-email");
    $user_pnumber       = secured_post("user-pnumber");
    $user_project       = secured_post("user-project");
    $user_location      = secured_post("user-location");
    $user_name          = secured_post("user-name");
    $user_pass          = secured_post("user-pass");

    if (multi_empty($user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass)) {

        $sql = "INSERT INTO users (user_role, user_firstname, user_lastname, user_email, user_pnumber, user_project, user_location, user_name, user_pass) VALUES (?,?,?,?,?,?,?,?,?)";
        $insert = $conn->prepare($sql);
        $insert->execute([$user_role, $user_firstname, $user_lastname, $user_email, $user_pnumber, $user_project, $user_location, $user_name, $user_pass]);


    } else {

        echo "Sorry.";

    }

}

0 个答案:

没有答案