我对php和数据库很陌生。我正在尝试创建一个表单,并从表单中获取数据并将其插入数据库中。我认为它可以成功地与数据库建立连接,但是当它应该从表格中获取数据并插入数据库中时什么也没发生。只需打开一个新页面,在其中写“成功连接”即可。
<div class="login">
<div class="box">
<form action="post" class="form-box">
<h3>Login</h3>
<input type="email" placeholder="E-mail">
<input type="password" placeholder="Password">
<input type="submit" value="Sign in" class="form-button">
<a href="#">Forgot yor password?</a>
</form>
</div>
</div>
<div class="register">
<div class="box">
<!-- <form action="core/init.php" method="post" class="form-box"> -->
<form method="post" class="form-box">
<h3>Create Account</h3>
<!-- <input type="text" placeholder="First Name*">
<input type="text" placeholder="Last Name*"> -->
<input type="email" placeholder="Email*" name="email">
<input type="password" placeholder="Password*" name="pass">
<input type="password" placeholder="Retype Password*" name="re_password">
<!-- <input type="checkbox" name="subscribe" class="input-inline"><label for="subscribe" >Subscribe here</label> -->
<input type="submit" value="Submit" class="form-button" name="registerUser">
</form>
</div>
</div>
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxx";
try {
$conn = new PDO("mysql:host=$servername;dbname=xxxxxx", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
if(isset($_POST['registerUser'])){
$email = $_POST['email'];
$password = $_POST['pass'];
$re_password= $_POST['re_password'];
if($password === $re_password){
$sql = "INSERT INTO users (email, pass) VALUES (?,?)";
$stmtinsert = $db->prepare($sql);
$result = $stmtinsert->execute([$email,$password]);
if($result){
echo 'Succes';
}else{
echo 'data error';
}
}else{
echo "Password doen't match";
}
}
?>
我发布了我的html和php代码。有谁能够帮助我?
谢谢