将随机字符串插入mysql表

时间:2015-06-02 08:01:28

标签: php mysql post

我尝试将变量randomString插入到表user2中的列代码中,但它不起作用。所有其他值都在表中,但生成的字符串不会。列代码的类型为varchar(8)。我的错误在哪里?

    <?php

session_start();
    $message = array();
    if (!empty($_POST)) {

                if(isset($_POST['f']['country']) )
{
    $country = $_POST['f']['country'];
}

function generateRandomString($length = 8) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$randomString = generateRandomString();


        if (
            empty($_POST['f']['username']) ||                       
            empty($_POST['f']['password']) ||
            empty($_POST['f']['password_again']) ||
            empty($_POST['f']['email']) ||
            empty($_POST['f']['firstname']) ||          
            empty($_POST['f']['lastname']) ||
            empty($_POST['f']['phone']) ||
            empty($_POST['f']['town']) ||
            empty($_POST['f']['street']) ||
            empty($_POST['f']['zip']) 
        ) { 

            $message['error'] = 'Es wurden nicht alle Felder ausgefüllt.';
        } else if ($_POST['f']['password'] != $_POST['f']['password_again']) {
            $message['error'] = 'Die eingegebenen Passwörter stimmen nicht überein.';
        } else {
            unset($_POST['f']['password_again']);
            $salt = ''; 
            for ($i = 0; $i < 22; $i++) { 
                $salt .= substr('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', mt_rand(0, 63), 1); 
            }
            $_POST['f']['password'] = crypt(
                $_POST['f']['password'],
                '$2a$10$' . $salt
            );

            $mysqli = @new mysqli('localhost', '', '', '');
            if ($mysqli->connect_error) {
                $message['error'] = 'Datenbankverbindung fehlgeschlagen: ' . $mysqli->connect_error;
            }
            $query = sprintf(
                "INSERT INTO user2 (username, password, email, firstname, lastname, phone, town, street, zip, country, code)
                SELECT * FROM (SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') as new_user
                WHERE NOT EXISTS (
                    SELECT username FROM user2 WHERE username = '%s'
                ) LIMIT 1;",
                $mysqli->real_escape_string($_POST['f']['username']),                               
                $mysqli->real_escape_string($_POST['f']['password']),                   
                $mysqli->real_escape_string($_POST['f']['email']),          
                $mysqli->real_escape_string($_POST['f']['firstname']),
                $mysqli->real_escape_string($_POST['f']['lastname']),
                $mysqli->real_escape_string($_POST['f']['phone']),
                $mysqli->real_escape_string($_POST['f']['town']),
                $mysqli->real_escape_string($_POST['f']['street']),
                $mysqli->real_escape_string($_POST['f']['zip']),
                $mysqli->real_escape_string($_POST['f']['country']),
                $mysqli->real_escape_string($randomString),
                $mysqli->real_escape_string($_POST['f']['username'])
            );
            $mysqli->query($query);
            if ($mysqli->affected_rows == 1) {
                $message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['f']['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
                header('Location: http://' . $_SERVER['HTTP_HOST'] . '//login.php');



                    $_SESSION = array(
                        'login' => true,
                        'user'  => array(
                            'username'  => $row['username']
                        )
                    );

            } else {

            }
            $mysqli->close();
        }
    } 
?>

2 个答案:

答案 0 :(得分:0)

您没有在任何地方使用功能generateRandomString。我现在还不太清楚它的用途。

如果$randomString有一些值(变量中的输入名称),那么您正在寻找

$_POST[$randomString]

如果您的input名称为randomString,则表示您正在寻找

$_POST['randomString']

无论如何,目前您正试图获得名为 $ randomString input的值。我打赌你没有这样的输入。

如果您的变量是单引号,则将其视为纯字符串。

如果您只想保存随机字符串,请使用您的函数

$randomString = generateRandomString();

并保存

$mysqli->real_escape_string($randomString),

答案 1 :(得分:0)

您的代码中几乎没有错误

1)$randomString = $_POST['$randomString'];更改为$randomString = $_POST['randomString'];

2)$mysqli->real_escape_string($_POST['$randomString']),更改为$mysqli->real_escape_string($_POST['randomString']),

您没有告诉我们您如何称呼function

generateRandomString()

你的代码??