数据未成功插入数据库

时间:2018-12-14 10:54:45

标签: php mysql sql database xampp

  • 文件保存在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");
?>

数据库和表: DB

输出: OutPut

不知道如何解决,感谢帮助!

1 个答案:

答案 0 :(得分:1)

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