【问题标题】:data not inserted successfully to DB数据未成功插入数据库
【发布时间】:2019-05-15 15:24:25
【问题描述】:
  • 保存在 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

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

【问题讨论】:

  • = 在您的表单 name "username" 上丢失。 $_POST 也必须大写。
  • @TarangP 电子邮件和密码 - varchar,名称 - 文本
  • 未定义索引 username 那是你的错误
  • @Roms 工作,非常感谢:)
  • @FireFun365 解决了部分问题,请参阅下面的答案

标签: php mysql sql database xampp


【解决方案1】:

未定义的索引username 应该是您在php 端的错误。

name: &lt;input type="text" name "username"&gt;

必须是:

name: &lt;input type="text" name="username"&gt;

然后在您的服务器端也使用准备好的语句。

那么你还有另一个错误:

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;
}
?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
相关资源
最近更新 更多