[英]Submit button in the html form is displaying the php script instead of sending form data to the db
我在 html 表單中的提交按鈕有這個問題,當我單擊它而不是將表單詳細信息發送到數據庫時,它會在新的 Z05B8C74CBD96FBF2DE4C1A352702FFF4Z 中顯示 php 腳本html 代碼:
<!DOCTYPE html>
<html>
<head>
<title>sign up</title>
<link href="sign in.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="signbox">
<h1>SIGN UP HERE</h1>
<form method="" action="connect.php" method="POST">
<p>First name:</p>
<input type="text" name="firstname" placeholder="enter your first name">
<p>Email address:</p>
<input type="text" name="email" placeholder="enter your email address">
<p>Password:</p>
<input type="password" name="password" placeholder="enter your password">
<p>Confirm password:</p>
<input type="password" name="password2" placeholder="confirm your password">
<input type="submit" value="sign in">
<a href="log in.html">already have an account?login here.</a>
</form>
</div>
</body>
</html>
PHP 代碼:
<?php
$firstname = $_POST['firstname'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
//database connection.
$conn = new mysqli('loacalhost','root','','','first_db');
if($conn->connect_error){
die('connection failed : '.$conn->connect_error);
}else{
$stmt = $conn->prepare("insert into users3(firstname, email, password, password2)
values(?, ?, ?, ?)");
$stmt->bind_param("ssss",$firstname, $email, $password, $password2);
$stmt->execute();
echo "registration complete...";
$stmt->close();
$conn->close();
}
?>
在您的表單標簽中有 2 個方法屬性,應更正為;
<form action="connect.php" method="POST">
而且在php代碼中, mysqli連接應該是;
$conn = new mysqli('localhost','root','','first_db');
其中四個參數分別是;
$conn= new mysqli("server name","username","password","database name");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.