尝试将用户设置表单字段添加到数据库但遇到问题

时间:2014-09-16 15:49:12

标签: php mysql mysqli

我有一个表单,用户将为我正在构建的自定义应用程序定义其用户设置。这个项目向我介绍了stmt,我不得不重新熟悉mysqli。

目前我提交的表格只有几个"选项"定义。我通过代码的随机回声被打印到屏幕上(用于测试)但是当我检查数据库时,只插入了一个选项(GmailID)选项。

屏幕上的错误消息显示为:

  

警告:mysqli_stmt :: bind_param():变量数量与第56行/home/website/public_html/portal/includes/process-user-settings.php中预准备语句中的参数数量不匹配< / p>

然而,查看我的代码看起来我有适量的变量和参数。就像我说的那样,它适用于GmailID但不适用于GmailPW并且不确定此错误是否已连接到某处?我包含我的代码文件以防万一。

error_reporting(-1);
ini_set('display_errors', 'On');

    echo 'Houston we are a go<br />';
    if (isset($_GET['GmailID'], $_GET['GmailPW'], $_GET['userID'])) {

    include_once 'db_connect.php';
    include_once 'psl-config.php';

    $error_msg = "";

    echo 'Info Set<br />';

        $GmailID = filter_input(INPUT_POST, 'GmailID', FILTER_SANITIZE_EMAIL);
        $GmailID = filter_var($GmailID, FILTER_VALIDATE_EMAIL);

        $GmailPW = filter_input(INPUT_POST, 'GmailPW', FILTER_SANITIZE_STRING);

        if (empty($error_msg)) {
            echo 'No Errors<br />';
            // Create a random salt
            $random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));
            echo $random_salt . '<br />';

            // Create salted password 
            $GmailPW = hash('sha512', $GmailPW . $random_salt);
            echo $GmailPW . '<br />';

            $user_id = $_GET['userID'];

            /* OPTIONS */
            $options = array(
                'GmailID' => $GmailID,
                'GmailPW' => $GmailPW
                );

            foreach($options as $key => $value) {
                echo 'Made it to the Foreach <br />';
                // Insert the options into the database 
                if ($insert_stmt = $mysqli->prepare("INSERT INTO user_settings (user_id, meta_key, meta_value, salt) VALUES ('$user_id', '$key', '$value', ?)")) {
                    $insert_stmt->bind_param('ssss', $user_id, $key, $value, $random_salt);
                    // Execute the prepared query.
                    if (! $insert_stmt->execute()) {
                        header('Location: ../index.php?err=insert');
                        exit();
                    }
                }
                echo ' We are done';
                header('Location: ../index.php?user=success');
                exit();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

使用占位符时,您的查询完全错误:

INSERT [...snip...] ('$user_id', '$key', '$value', ?)")) {
                                                   ^---one single placeholder

您的查询中有完全 ONE 占位符,以及3个直接插入的变量,这意味着您将对SQL注入攻击进行打开。

应该是

INSERT [...snip...] (?, ?, ?, ?)

答案 1 :(得分:1)

这是最终为我工作的东西,希望它可以帮助别人=)

$options = array(
            'GmailID' => $GmailID,
            'GmailPW' => $GmailPW,
            'WebmailID' => $WebmailID,
            'WebmailPW' => $WebmailPW,
            'ProfileIMG' => $ProfileIMG
            );

        foreach($options as $key => $value) {

            // Insert the options into the database 
            if ($insert_stmt = $mysqli->prepare("INSERT INTO user_settings (user_id, meta_key, meta_value, salt) VALUES (?, ?, ?, ?)")) {
                $insert_stmt->bind_param('ssss', $user_id, $key, $value, $random_salt);
                // Execute the prepared query.
                if (! $insert_stmt->execute()) {
                    header('Location: ../index.php?err=insert');
                    exit();
                }
            } 

            //Success
            header('Location: ../index.php?user=success');
            exit();
        }