gpt4 book ai didi

php - 将用户信息插入mysql数据库

转载 作者:行者123 更新时间:2023-11-29 12:28:01 25 4
gpt4 key购买 nike

我为移动设备创建了一个社交网络,并尝试将一些用户信息与文件“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 ,

登录.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>
';
}
?>

设置.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、residence 和 radio-choice-h-2,因为这些变量仅在提交表单后才存在。 (http://php.net/manual/en/reserved.variables.post.php)

您应该在运行数据库查询之前检查这些变量是否存在。像这样的事情:

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

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

关于php - 将用户信息插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28013906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com