将表单数据插入mysql表

时间:2015-05-29 14:16:21

标签: php html mysql twitter-bootstrap mysqli

我尝试将表单数据放入mysql表中,但它无法正常工作。密码和我***是对的。复选框和国家/地区现在是次要的。

  

php:警告:sprintf():参数太少

     

警告:mysqli :: query():空查询

mysql表:

CREATE TABLE IF NOT EXISTS 'users' (
  'id'       INT(11) NOT NULL AUTO_INCREMENT,
  'username' VARCHAR(255) NOT NULL,
  'password' VARCHAR(255) NOT NULL,
  'email' VARCHAR(255) NOT NULL,
'firstname' VARCHAR(255) NOT NULL,
'lastname' VARCHAR(255) NOT NULL,
'phone' INT(255) NOT NULL,
'town' VARCHAR(255) NOT NULL,
'street' VARCHAR(255) NOT NULL,
'zip' INT(255) NOT NULL,
  PRIMARY KEY ('id'),
  UNIQUE KEY 'username' ('username')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    <?php


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

        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', 'root', '***', '***');
            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)
                SELECT * FROM (SELECT '%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->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'] . '/socialad/login.php');
            } else {

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



<!DOCTYPE html>
<html>
    <head>
        <title></title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <!-- Bootstrap -->
            <link href="css/bootstrap.min.css" rel="stylesheet">
            <script type="text/javascript" type="js/bootstrap.min.js"></script>
            <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
            <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
            <!--[if lt IE 9]>


            <![endif]-->
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>


    </head>
    <body>
        <div class="bs-example">
    <h1>Sign Up as a adsearcher</h1>
    <form action="./register1.php" method="post" class="form-horizontal">

        <div class="form-group">
            <label class="control-label col-xs-3" for="username">Username:</label>
            <div class="col-xs-5">
                <input name="f[username]" type="text" class="form-control" id="inputusername" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="inputPassword">Password:</label>
            <div class="col-xs-5">
                <input name="f[password]" type="password" class="form-control" id="inputPassword" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="confirmPassword">Confirm Password:</label>
            <div class="col-xs-5">
                <input name="f[password_again]" type="password" class="form-control" id="confirmPassword" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="inputEmail">Email:</label>
            <div class="col-xs-5">
                <input name="f[email]" type="email" class="form-control" id="inputEmail" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="firstName">First Name:</label>
            <div class="col-xs-5">
                <input name="f[firstname]" type="text" class="form-control" id="firstName" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="lastName">Last Name:</label>
            <div class="col-xs-5">
                <input name="f[lastname]" type="text" class="form-control" id="lastName" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="phoneNumber">Phone:</label>
            <div class="col-xs-5">
                <input name="f[phone]" type="tel" class="form-control" id="phoneNumber" >
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-xs-3" for="adress">Adress</label>

        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="town">Town:</label>
            <div class="col-xs-5">
                <input name="f[town]" type="text" class="form-control" id="town" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="street">Street:</label>
            <div class="col-xs-5">
                <input name="f[street]" type="text" class="form-control" id="street" >

            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-3" for="ZipCode">Zip Code:</label>
            <div class="col-xs-5">
                <input name="f[zip]" type="text" class="form-control" id="ZipCode" >
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-xs-3" for="country">Country:</label>            
            <div class="col-xs-3">
                <select class="form-control">

                    <option value="0">Please choose...</option>
                    <option value="0">--------------------------</option>
                                            <option value="221">United Kingdom</option>
                                            <option value="222">United States</option>


                                                            </select>
                </select>
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-offset-3 col-xs-9">
                <label class="checkbox-inline">
                    <input name="tos" value="accepted" type="checkbox">  I agree to the <a href="#">Terms and Conditions</a>.
                </label>
            </div>
        </div>
        <br>
        <div class="form-group">
            <div class="col-xs-offset-3 col-xs-9">
                <input name="submit" type="submit" value="Register" class="btn btn-primary" >
                <input type="reset" class="btn btn-default" value="Reset">
            </div>
        </div>

    </form>
</div>

    </body>
</html>

1 个答案:

答案 0 :(得分:2)

很明显,你有10个%s并且你只提供了9个参数........