我无法通过php将我的html表单输入数据存储在mysql数据库中。当我填写表单并提交时,我的register.php页面是空白的,成功或错误消息都没有回显到页面上。
注意:是的我有root密码和数据库名称。两者都在我的代码中正确填写,我刚刚删除了这篇帖子。
编辑:半结肠修复,仍有未知错误。html表格:
<form action="register.php" method="POST">
<input type="text" name="first_name" placeholder="first name" /><br /><br />
<input type="text" name="last_name" placeholder="last name" /><br /><br />
<input type="text" name="email" placeholder="email address" /><br /><br />
<input type="text" name="password" placeholder="password" /><br /><br />
<input type="submit" name="register" value="Sign Up" />
</form>
register.php
<?php
if (isset($_POST['register'])) {
$host = "localhost";
$username = "root";
$password = "";
$database = "";
$conn = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$first_name = mysqli_real_escape_string($_POST['first_name']);
$last_name = mysqli_real_escape_string($_POST['last_name']);
$email = mysqli_real_escape_string($_POST['email']);
$password = mysqli_real_escape_string($_POST['password']);
$date = date("Y-m-d");
$password = md5($password);
$sql = "INSERT INTO users (first_name, last_name, email, password, sign_up_date) VALUES ('$first_name', '$last_name', '$email', '$password', '$date')";
if (!mysqli_query($sql)) {
die('Error: ' .mysqli_error());
}
echo "Entry added successfully";
mysqli_close($conn);
}
?>
答案 0 :(得分:7)
试试这个:
<?php
if (isset($_POST['register'])) {
$host = "localhost";
$username = "root";
$password = "";
$database = ""; //also added semi colon and what database name
$conn = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$first_name = mysqli_real_escape_string($conn,$_POST['first_name']); //I've added $conn inside mysqli_real_escape_string expected 1st param
$last_name = mysqli_real_escape_string($conn,$_POST['last_name']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$date = date("Y-m-d");
$password = md5($password);
$sql = "INSERT INTO users (first_name, last_name, email, password, sign_up_date) VALUES ('$first_name', '$last_name', '$email', '$password', '$date')";
$query = mysqli_query($conn, $sql); // I think this will add data in your database.
if (!$conn) { //I change this from mysql_query($sql)
die('Error: ' .mysqli_error());
} else {
echo "Entry added successfully";
mysqli_close($conn);
}
}
答案 1 :(得分:0)
错误在这一行:
$database = ""
你忘记了;
您需要设置数据库名称,数据库名称不能为空。
$database = "Your_DB_Name";