gpt4 book ai didi

php - 尝试通过 HTML 和 php 将数据插入 Mysql 数据库时出错

转载 作者:行者123 更新时间:2023-11-30 21:51:36 25 4
gpt4 key购买 nike

我正在尝试通过 PHP 文件通过 Html 将用户名和密码输入 MySql 数据库。这是 html 代码。

<div id="JoinModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal Content -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Join </h4>
</div>
<div class="modal-body">
<form action="demo4.php" method="POST">
<div class="form-group">
<label><b>Email address:</b></label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email" class="form-control input-sm">
</div>
<div class="form-group">
<label><b>Password:</b></label>
<input type="password" id="password" name="password" placeholder="Enter Password" class="form-control input-sm">
</div>
<div class="form-group">
<label class="sr-only" for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Repeat Password" class="form-control input-sm">
</div>
<div class="checkbox">
<label style="color:black;">
<input type="checkbox"><small> Remember Me</small>
</label>
</div>
<button type="submit" id="signIn" class="btn btn-info btn-sm">Sign In</button>
<button type="button" id="cancelModal" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
</form>
</div>
</div>
</div>
</div>

这是PHP代码:

    <?php  // <-- if you don't open the php tag, your code won't work
$servername="localhost";
$dbname="schema1";
$password="1234";
$user="root";
$port ="3306";


$conn = new mysqli($servername,$user,$password);



if ($conn->connect_error) {
die("connection failed:" .$conn->connect_error);
}

echo "Connected successfully to Mysql server!";


$db_selected = mysqli_select_db($conn,$dbname);

if (!$db_selected){
die("Cant use db: " .mysql_error());
}

echo 'Connected successfully to database';

// $email = mysqli_real_escape_string($conn,$_REQUEST)['email'];
// echo ( "email is:" .$email) ;

// $password = mysqli_real_escape_string($conn,$_REQUEST)['password'];

$email = $_POST['email'];
$password = $_POST['password'];

$sql = "INSERT INTO Members (email,password)
VALUES ('$email', '$password')";

if (!mysqli_query($conn,$sql)) {
die('Error!!: ' . mysqli_error($conn));
}
else {
echo "Success:";
}
$conn->close();
?>

当我在浏览器中输入用户名和密码时出现此错误:(它只显示 php 文件)

尝试通过 HTML 和 php 将数据插入 Mysql 数据库时出错

connect_error) { die("connection failed:" .$conn->connect_error); } echo "Connected successfully to Mysql server!"; $db_selected = mysqli_select_db($conn,$dbname); if (!$db_selected){ die("Cant use db: " .mysql_error()); } echo 'Connected successfully to database'; // $email = mysqli_real_escape_string($conn,$_REQUEST)['email']; // echo ( "email is:" .$email) ; // $password = mysqli_real_escape_string($conn,$_REQUEST)['password']; $email = $_POST['email']; $password = $_POST['password']; $sql = 'INSERT INTO `Members` (`email`,`password`) VALUES (:email, :password)'; if (!mysqli_query($conn,$sql)) { die('Error!!: ' . mysqli_error($conn)); } else { echo "Success:"; } $conn->close(); ?>

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

连接失败,因为你忘了写_connect没错

        $conn = mysqli_connect($servername,$user,$password);

关于php - 尝试通过 HTML 和 php 将数据插入 Mysql 数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46970007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com