php - 数据未成功插入数据库
问题描述
- 保存在 htdocs 目录中的文件** 当我运行该文件时,我在 url 中将其更改为:localhost/aa.html
html 文件 (aa.html):
<!DOCTYPE html>
<html>
<head>
<title>RegPage</title>
</head>
<body>
<form action ="bb.php" method="post">
name: <input type="text" name "username">
<br/>
email: <input type="text" name ="email">
<br/>
password: <input type="password" name="password">
<input type = "submit" value = "insert">
</form>
</body>
</html>
php 文件 (bb.php):
<?php
$con=mysqli_connect('localhost','root','');
if(!con)
{
echo 'not connected to server';
}
if (!mysqli_select_db ($con,'pilot'))
{
echo 'database not selected';
}
$name=$_post['username'];
$email=$_post['email'];
$password=$_post['password'];
$sql="insert into dbinvestor (email,password,name) values ('$email','$password', '$name')";
if (!mysqli_query($con,$sql))
{
echo 'not inserted';
}
else
{
echo 'inserted succesfuly';
}
header ("refresh:2;url=aa.html");
?>
数据库和表: 数据库
输出: 输出
不知道怎么解决,谢谢帮助!
解决方案
未定义的索引username
应该是您在 php 方面的错误。
name: <input type="text" name "username">
一定是 :
name: <input type="text" name="username">
然后还在服务器端使用准备好的语句。
然后你还有另一个错误:
if(!con)
未定义的常数con
这应该是:
if(!$con)
最后不要将密码存储为纯文本使用 password_hash() 和 password_verify()
<?php
$con=mysqli_connect('localhost','root','');
if(!$con)
{
echo 'not connected to server';
}
if (!mysqli_select_db ($con,'pilot'))
{
echo 'database not selected';
}
$name=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
//hash password
$hash = password_hash($password,PASSWORD_DEFAULT);
$sql="insert into dbinvestor (email,password,name) values (?,?,?)";
$stmt = $con->prepare($sql);
$stmt->bind_parm("sss",$email,$hash,$name);
if($stmt->execute()){
echo 'inserted succesfuly';
}else{
echo 'not inserted';
echo $stmt->error;
}
?>
推荐阅读
- node.js - 对于 node-postgres(“pg”库),会话变量在行级安全性方面是否安全?
- javascript - 如何使用js模拟输入那些元素。谢谢
- javascript - “对象不是函数”Polymer 3 包装器
- python - Pydub wma to mp3 修复“库配置不匹配”
- c - 在我的多线程程序中,pthread_cond_signal 似乎丢失了?
- android - RxJava Observable 和改造 API 调用
- python - Pyspark - 根据一行中的条件过滤掉多行
- iphone - Xamarin Forms WebView 问题与 iPhone 上的摄像头有关
- node.js - nodejs 中的 Hyperledger Fabric nodejs 链代码单元测试,
- python - 如何从详细的变化数据中获取 Python 中的季度 S&P500 成分股?