簡體   English   中英

將用戶信息插入mysql數據庫

[英]Insert user info into 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();
?>

加載頁面后,$ _ POST變量中不存在變量age,駐留和radio-choice-h-2,因為這些變量僅在提交表單后存在。 http://php.net/manual/en/reserved.variables.post.php

您應該在運行數據庫查詢之前檢查這些變量是否存在。 像這樣:

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

通常,您應該檢查變量是否存在並且包含正確的值。 特別是在使用表格時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM