html表单未链接到php文件
我已经创建了一个html表单,并希望将其连接到xampp上的数据库。我已经创建了数据库和表,但是当我单击submit时,php文件无法工作。我是网络开发新手。如果我只是运行html表单未链接到php文件,php,html,mysql,xampp,Php,Html,Mysql,Xampp,我已经创建了一个html表单,并希望将其连接到xampp上的数据库。我已经创建了数据库和表,但是当我单击submit时,php文件无法工作。我是网络开发新手。如果我只是运行http://localhost/connect.php然后它会工作,但当我在表单上单击submit时,它会重定向到file:///F:/Xampp/htdocs/connect.php打开并显示错误。这是我的表单的html代码 <form method="post" onSubmit="return validateF
http://localhost/connect.php
然后它会工作,但当我在表单上单击submit时,它会重定向到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();
?>
在
和
之间的连接中没有错误<代码>
似乎未正确连接到数据库
注册
:在“键入”之后没有“=”。我这样做了,效果很好。谢谢
action="http://localhost/connect.php"
请检查您的PHP版本,如果可能的话,也用这些信息更新问题。如果您使用的是
5.2.9
和5.3.0
之前的PHP版本,则必须使用以下代码,因为$connect\u错误一直到PHP5.2.9
和5.3.0
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
而不是
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
请参考的官方页面,您无法访问PHP文件,因为文件://
。即使我的html和PHP文件都位于同一文件夹中,我的提交按钮也会将其重定向到此地址,即F:\Xampp\htdocstried操作=”http://localhost/connect.php“
?onSubmit=“return validateForm();”
你能发布一下validateForm()
的功能吗?不。当我刚刚运行php文件时,我成功地连接到了dbNew record。数据库显示新数据。主要原因是您通过文件://
访问.html
,这是错误的。您应该通过完整的URLhttp://localhost/myhtml.html
噢。我不知道。谢谢