【发布时间】:2020-08-09 17:19:12
【问题描述】:
我创建了一个 html 表单并希望将其连接到 xampp 上的数据库。我已经创建了数据库和表,但是当我单击提交时,php 文件不起作用。我是网络开发的新手。如果我只是运行http://localhost/connect.php,那么它可以工作,但是当我单击表单上的提交时,它会重定向到file:///F:/Xampp/htdocs/connect.php 打开并显示错误。这是我的表单的 html 代码。
<form method="post" onSubmit="return validateForm();" action="connect.php">
<input type="text" id="fname" name="fname" class="input-box" maxlength="20" pattern="[A-Z]{1}[a-z]{2,}"placeholder="First Name" required autofocus>
<input type="text" id="lname" name="lname" class="input-box" maxlength="20" pattern="[A-Z]{1}[a-z]{2,}" placeholder="Last Name" required>
<br>
<br>
<input type="email" id="dataemail" name="dataemail" class="input-box" style="width: 48%;" placeholder="Your Email ID" required>
<br><br>
<input type="password" id="datapass" name="datapass" class="input-box" style="width: 48%;" placeholder="Password" required>
<br><br>
<input type="text" id="dob" name="dob" class="input-box" style="width: 48%;" placeholder="Date of Birth" onfocus="(this.type='date')" min="1901-01-01" max="1999-12-31" required>
<br>
<p style="font-size:150%;">
<input type="radio" id="male" name="gender" value="Male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label>
<input type="radio" id="other" name="gender" value="Other">
<label for="other">Other</label>
</p>
<p><span><input type="checkbox" required></span> I agree to these <a href= "##">terms and conditions </a></p>
<button type:"button" class="signupbutton">Sign Up</button>
<hr>
<p class="or">OR</p></form>
这是我的php代码
<?php
$dbhost = 'localhost' ;
$username = 'root' ;
$password = '' ;
$db = 'task';
$fname = filter_input(INPUT_POST,'fname');
$lname = filter_input(INPUT_POST,'lname');
$dataemail = filter_input(INPUT_POST,'fdataemail');
$datapass = filter_input(INPUT_POST,'datapass');
$dob = filter_input(INPUT_POST,'dob');
$gender = filter_input(INPUT_POST,'gender');
$conn =new mysqli("$dbhost", "$username", "$password", "$db" );
echo "Connected to db";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `signup` (`First name`, `Last name`, `Email ID`, `Password`, `Date of Birth`, `Gender`)
VALUES ('$fname','$lname','$dataemail','$datapass','$dob','$gender')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
【问题讨论】:
-
您不能以
file://..的身份访问PHP文件。 -
我的提交按钮将它重定向到这个地址,即使我的 html 和 php 文件都在同一个文件夹中,即 F:\Xampp\htdocs
-
试过
action="http://localhost/connect.php"? -
onSubmit="return validateForm();"你能发布validateForm()的作用吗?