我无法通过php将我的html表单输入数据存储在mysql数据库中.当我填写表单并提交时,我的register.php页面是空白的,成功或错误消息都没有回显到页面上.注意:是的我有root密码和数据库名称.两个都在我的代码中正确填写...
我无法通过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);
}
?>
解决方法:
试试这个:
<?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);
}
}
沃梦达教程
本文标题为:php – 将html表单数据发送到mysql数据库
猜你喜欢
- 浅谈Vue3的几个优势 2022-07-07
- 通过手写instanceof理解原型链示例详解 2023-08-08
- javascript Blob对象实现文件下载 2023-08-12
- php – Laravel – 保存HTML Dom-Parser对数据库的响应 2023-10-26
- 深入浅出JavaScript前端中的设计模式 2023-07-09
- ajax异步实现文件分片上传实例代码 2023-02-23
- IE8用ajax访问不能每次都刷新的问题 2023-01-20
- mysql / php – 在数据库中存储html模板 2023-10-25
- 30.vue的安装 2023-10-08
- 关于extjs:在网格上实现beforeedit监听器 2022-09-15