我无法通过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数据库
基础教程推荐
猜你喜欢
- 使用Ajax或Easyui等框架时的Json-lib的处理方案 2023-02-14
- JS中的常见数组遍历案例详解(forEach, map, filter, sort, reduce, every) 2023-07-10
- 【vue】 export、export default、import的用法和区别 2023-10-08
- TS中最常见的声明合并(接口合并) 2023-08-08
- 一个导航条布局的简单写法 2022-10-16
- SmartPlant3D VUE解析 2023-10-08
- AJAX实现跨域的三种方法(代理,JSONP,XHR2) 2022-12-15
- 图文示例讲解useState与useReducer性能区别 2023-07-10
- 实现Vue路由切换的监听 2023-10-08
- CSS做一个超链接的陷下效果 2022-10-16