dtj88302 2015-01-18 19:34
浏览 94
已采纳

将用户信息插入mysql数据库

I create an social network for mobile devices and try to insert some user information with the file "settings.php" into my database. But when I run "settings.php" chrome show me errors( I comment in the file where the errors are showing. Have anyone a hint what I should change?

My Database:

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条回答 默认 最新

  • du59131 2015-01-18 19:46
    关注

    upon loading the page the variables age, residence and radio-choice-h-2 do not exist in the $_POST variable as these variables only exist after submitting a form. (http://php.net/manual/en/reserved.variables.post.php)

    You should check if these variable exist before running a database query. Something like this:

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

    You should generally check if a variable exists and contains correct values. Especially when using forms.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图