我创建了一个registration.php登录user.php一个error.php和一个server.php,其中的错误验证和服务器将我的php表单连接到数据库。 login.php工作正在说错误的ID和/或密码,而注册表不起作用。当我点击提交它就像刷新一样,没有保存到数据库。尝试3天,无法理解为什么。也许是我的p.c问题?我正在使用XAMPP和phpmyadmin以及dreamweaver。
这是我的register.php。
<?php include('server.php') ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link rel="stylesheet" type="text/css" href="forms.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Name:</label>
<input type="text" name="name">
</div>
<div class="input-group">
<label>Surname:</label>
<input type="text" name="surname">
</div>
<div class="input-group">
<label>Password:</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm Paswword:</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<label>Student ID:</label>
<input type="text" name="studentid">
</div>
<div class="input-group">
<label>Email:</label>
<input type="text" name="email">
</div>
<div class="input-group">
<label>Course:</label>
<input type="text" name="course">
</div>
<div class="input-group">
<center><button type="submit" name="register" class="btn">Register</button></center>
</div>
<p>
Already a registered student? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
这是我的server.php
<?php
session_start();
// variable declaration
$name = "";
$surname = "";
$email = "";
$studentid = "";
$password_1 ="";
$password_2 = "";
$course = "";
$errors = array();
$_SESSION['success'] = "";
// connect to database
$db = mysqli_connect('localhost', 'root', '', 'registration');
// if register button clicked receive all inputs from the form
if (isset($_POST['reg_user'])) {
$name = mysqli_real_escape_string($db, $_POST['name']);
$surname = mysqli_real_escape_string($db, $_POST['surname']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$studentid = mysqli_real_escape_string($db, $_POST['studentid']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_1 = md5($password_1);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
$password_2 = md5($password_2);
$course = mysqli_real_escape_string($db, $_POST['course']);
// ensure that form fields are filled properly
if (empty($name)) {
array_push($errors, "Name is required");
}
if (empty($surname)) {
array_push($errors, "Surame is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($studentid)) {
array_push($errors, "Student ID is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if (empty($course)) {
array_push($errors, "Course is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
if (count($errors) == 0) {
$password = md5($password_1); //encrypt the password before saving in the database
$query = "INSERT INTO users (id, name, surname, email, studentid, password, course)
VALUES(0,'$name','$surname', '$email', '$studentid' '$password', '$course')";
mysqli_query($db, $query);
$_SESSION['studentid'] = $studentid;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
// login user
if (isset($_POST['login_user'])) {
$studentid = mysqli_real_escape_string($db, $_POST['studentid']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($studentid)) {
array_push($errors, "Student ID is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE studentid='$studentid' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['studentid'] = $studentid;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong Student ID or Password. Please try again.");
}
}
}
?>
答案 0 :(得分:0)
问题出在您的查询中您可以使用surname
代替username
。
更改查询
从这个
$query = "INSERT INTO users (name, surname, email, studentid, password, course)
VALUES(0,'$name','$surname', '$email', '$studentid' '$password', '$course')";
到此
$query = "INSERT INTO users (id, name, surname, email, studentid, password, course)
VALUES('$name','$username', '$email', '$studentid' '$password', '$course')";
代码中有很多拼写错误。请放松并检查所有拼写。
答案 1 :(得分:0)
你的按钮名称与php中的名称不同
//previous code button clicked for register
if (isset($_POST['reg_user'])) {}
而不是
//button clicked for register
if (isset($_POST['register'])) {}
以及您的查询 如果你的“id”是一个自动增量,把它留空而不是0,你也可以这样做
$query = "INSERT INTO users (name, surname, email, studentid, password,
course)VALUES('$name','$surname', '$email', '$studentid' '$password', '$course')";
更新以帮助任何有类似问题的人
对于可能遇到类似代码挑战的任何人,请检查每个半列,逗号和点是否应该在哪里。
您也可以通过回显值来调试,以了解问题可能来自哪里。
同时阅读评论,您可以从中获取所需内容。 希望这有帮助。