首页 > 解决方案 > 数据未成功插入数据库

问题描述

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");
?>

数据库和表: 数据库

输出: 输出

不知道怎么解决,谢谢帮助!

标签: phpmysqlsqldatabasexampp

解决方案


未定义的索引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;
}
?>

推荐阅读