将用户信息插入mysql数据库

时间:2015-01-18 19:34:14

标签: php html mysql database

我为移动设备创建了一个社交网络,并尝试将一些带有“settings.php”文件的用户信息插入到我的数据库中。但是当我运行“settings.php”时,chrome会显示错误(我在错误显示的文件中发表评论。有人提示我应该改变什么吗?

我的数据库:

CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR NOT NULL ,
`password` VARCHAR NOT NULL ,
`picture` VARCHAR NOT NULL ,
`age` INT NOT NULL ,
`residence` VARCHAR NOT NULL ,
`status` VARCHAR NOT NULL ,
`sex` VARCHAR NOT NULL ,

的login.php

 <?php
    if (!empty($_POST)) {
        if (
            empty($_POST['f']['username']) ||
            empty($_POST['f']['password'])
        ) {
            $message['error'] = 'Es wurden nicht alle Felder ausgefüllt.';
        } else {
            $mysqli = @new mysqli('localhost', 'root', 'pw', 'database');
            if ($mysqli->connect_error) {
                $message['error'] = 'Datenbankverbindung fehlgeschlagen: ' . $mysqli->connect_error;
            }
            $query = sprintf(
                "SELECT username, password FROM users WHERE username = '%s'",
                $mysqli->real_escape_string($_POST['f']['username'])
            );
            $result = $mysqli->query($query);
            if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                if (crypt($_POST['f']['password'], $row['password']) == $row['password']) {
                    session_start();

                    $_SESSION = array(
                        'login' => true,
                        'user'  => array(
                            'username'  => $row['username']
                        )
                    );
                    $message['success'] = '';
                    header('Location: http://' . $_SERVER['HTTP_HOST'] . '/anyask/main.php');
                } 
            } 
            $mysqli->close();
        }
    } 
?>  
<html>
<head>
    <meta charset="UTF-8" /> 
    <title>
        HTML Document Structure
    </title>
    <link rel="stylesheet" type="text/css" href="style1.css" />

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>


    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

</head>
<body>

<div id="wrapper">

    <form name="login-form" class="login-form" action="./login.php" method="post">

        <div class="header">
        </div>

        <div class="content">
        <label for="username"></label>
        <input name="f[username]" type="text" class="input username" placeholder="Username" id="username"
        <?php 
                    echo isset($_POST['f']['username']) ? ' value="' . htmlspecialchars($_POST['f']['username']) . '"' : '' ?>/>
        <label for="password"></label>
        <input name="f[password]" type="password" class="input password" placeholder="Password" id="password" />

        </div>

        <div class="footer">
        <input type="submit" name="submit" value="Login" class="button" data-theme="b"/>
        <a href="./register.php">Register</a>
        </div>

    </form>

</div>
<div class="gradient"></div>


</body>
</html>

auth.php

    <?php
    session_start();
    session_regenerate_id();

    if (empty($_SESSION['login'])) {
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/login.php');
        exit;
    } else {
        $login_status = '
            <div style="border: 1px solid black">
                Sie sind als <strong>' . htmlspecialchars($_SESSION['user']['username']) . '</strong> angemeldet.<br />
                <a href="./logout.php">Sitzung beenden</a>
            </div>
        ';
    }
?>

的settings.php

 <?php require_once './auth.php'; ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>main</title>
        <link rel="stylesheet" type="text/css" href="mainstyle.css" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>


    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    </head>
    <body>
        <div data-role="header" data-theme="b" data-position="fixed" data-tap-toggle="false" > 
            <h1 class="ui-title" role="heading" aria-level="1">anyask</h1>

    </div>

    <div data-role="main" class="ui-content">
     <div id="wrapper">

    <form name="login-form" class="login-form" action="./settings.php" method="post">

        <div class="header">
        </div>

        <div class="content">
        <label for="age"></label>
        <input name="age" type="number" class="input age" placeholder="age" id="age"/>

        <label for="residence"></label>
        <input name="residence" type="text" class="input residence" placeholder="residence" id="residence" />


        <fieldset data-role="controlgroup" data-type="horizontal">
        <legend>sex:</legend>
        <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2a" value="man" checked="checked">
        <label for="radio-choice-h-2a">man</label>
        <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2b" value="woman">
        <label for="radio-choice-h-2b">woman</label>

        </fieldset>
        profile picture: 
        <input name="attachment" type="file" id="attachment" /><br>
        </div>

        <div class="footer">
        <input type="submit" name="submit" value="save" class="button" data-theme="b"/>

        </div>

    </form>

</div>
<div class="gradient"></div>

        </div>




    </body>
</html>
<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// upload picture

// the following 3 lines are showed as error 
$age=$_POST['age'];
$residence=$_POST['residence'];
$sex=$_POST['radio-choice-h-2'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(age, residence, radio-choice-h-2)VALUES('$age', '$residence', '$sex')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/anyask/main.php');
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

1 个答案:

答案 0 :(得分:1)

加载页面时,变量age,residence和radio-choice-h-2在$ _POST变量中不存在,因为这些变量仅在提交表单后才存在。 (http://php.net/manual/en/reserved.variables.post.php

在运行数据库查询之前,应检查这些变量是否存在。像这样:

if (isset($_POST['age'])) {
    // DO SOMETHING
}

通常应检查变量是否存在且包含正确的值。特别是在使用表格时。