gpt4 book ai didi

php - 使用 "PHP form"连接/提交到 MySQL 失败

转载 作者:行者123 更新时间:2023-11-29 02:14:33 24 4
gpt4 key购买 nike

我有两个问题。基本故事:我创建了一个简单的注册和登录系统。
问题 1: 如果我尝试注册一个新帐户,它会显示“用户注册失败”。目前它应该说是因为 mysql 无法从表单中获取正确的信息。但问题是我不知道为什么。一切似乎都是正确的...
问题 2: 如果我尝试使用现有帐户登录,那么浏览器似乎只是刷新页面,没有其他任何东西......
使用php代码注册:

 <?php
require ('insert.php');
// If values posted, insert into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

// nimi refers to name, it's correct
$query = "INSERT INTO `user` (nimi, email, username, password)
VALUES('$name', '$email', '$username', '$password')";

//POST retrieves the data.
$result = mysqli_query($connection, $query);

if($result){
$smsg = "User Created Successfully.";
} else {
$fmsg = "User Registration Failed";
}
}

mysqli_close($connection);
?>
<html>
...
<body>
...
<div>

<form method="POST" class="form-horizontal" role="form">

<!-- Status, how registering went -->
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>


<!-- Registration form starts -->
<h2>Form</h2><br>


<label for="Name"></label>
<input name="name" type="text" id="name" maxlength="40" placeholder="Ees- ja perenimi" class="form-control" autofocus> <!-- lopp -->

<label for="email"></label>
<input name="email" type="email" id="email" maxlength="65" placeholder="Email" class="form-control"> <!-- lopp -->


<label for="Username"></label>
<input name="username" type="text" id="userName" maxlength="12" placeholder="Kasutajatunnus/kasutajanimi" class="form-control" required> <!-- lopp -->

<label for="Password"></label>
<input name="password" type="password" id="password" maxlength="12" placeholder="Parool" class="form-control" required>

<button type="submit" class="btn btn-primary btn-block">Join</button>
</form> <!-- /form -->

</div> <!-- ./container -->
...
</body>
</html>

登录:

<?php
session_start();
require ('insert.php');

//Is username and password typed?
if (isset($_POST['username']) and isset($_POST['password'])){
//Making vars from inputs
$username = $_POST['username'];
$password = $_POST['password'];
//Checking existent of values.
$query = "SELECT * FROM `liikmed`
WHERE username='$username'
and password='$password'";

$result = mysqli_query($connection, $query)
or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
//3.1.2 If values equal, create session.
if ($count == 1){
$_SESSION['username'] = $username;
} else {
//If credentials doesn't match.
$fmsg = "Invalid Login Credentials.";
}
}
//if user logged in, welcome with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hai " . $username . "";
echo "This is the Members Area";
echo "<a href='logout.php'>Logout</a>";

}else{}
?>

<html>
...
<body>
...
<div id="bg"></div>


<form method="POST" class="form-horizontal">
<h2>Login</h2><br>

<label for="User"></label>
<input name="username" type="text" maxlength="15" placeholder="Username" class="form-control" required autofocus>

<label for="Password"></label>
<input name="password" type="password" maxlength="50" placeholder="Password" class="form-control" required autofocus>

<button type="submit" class="btn btn-primary btn-block">Enter</button>

</form>
</div>
...
</body>
</html>

最后是 php 数据库连接文件(称为 insert.php):

<?php
$connection=mysqli_connect("localhost","root","pw");
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'my_database');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>

最佳答案

首先,在您的登录 PHP 代码中,您只启动了一个 session,但您没有告诉如果登录成功从哪里定向。将 header 添加到代码中。也就是;

if ($count == 1){
$_SESSION['username'] = $username;
header("Location: page.php"); //the page you want it to go to
}

您的注册 PHP 代码看起来没问题。如果您在那里拼错了任何内容,请检查您的数据库表。

关于php - 使用 "PHP form"连接/提交到 MySQL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42204375/

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