[英]Error in php mysql connection using xampp
I'm trying to connect a HTML page using PHP form to MySQL database using Xampp. 我正在尝试使用Xampp将使用PHP表单的HTML页面连接到MySQL数据库。 The HTML page is working fine but as soon as I press submit button a page with distinct characters appears, can anybody help with the problem?![php page][1] HTML页面运行良好,但是当我按下“提交”按钮时,会出现一个包含不同字符的页面,有人可以帮助解决该问题吗?![php页面] [1]
HTML code: HTML代码:
<html>
<body>
<body bgcolor="black">
<font color="white">
<form action="a.php" method="post">
<center>
<h1><font color = "gold"><marquee>Welcome to online bus booking!!!</h1></marquee> </font color>
<h3>
First Name:<br><input type = "text" name = "fnm"><br>
Last name:<br><input type = "text" name = "lnm"><br>
Mobile:<br><input type = "text" name = "mob"><br>
Age:<br><input type = "number" name = "age"><br>
Source:<input type = "text" name = "src">
Destination:<input type = "text" name = "des"><br>
<font color="orange">Passenger Address:<br></font color>
Street:<br><input type = "text" name = "str"><br>
Area:<br><input type = "text" name = "area"><br>
City:<br><input type = "text" name = "cty"><br>
<input type = "submit">
</h3>
</center>
</form>
</body>
</html>
PHP code: PHP代码:
<?php
$con=mysqli_connect("localhost","root","","testdb");
if(mysqli_connect_error())
{
echo "FAILED" . mysqli_connect_error();
}
$sql="INSERT INTO exp VALUES('$_POST[fnm]', '$_POST[lnm]', $_POST[mob], $_POST[age], '$_POST[src]', '$_POST[des]', '$_POST[str]', '$_POST[area]', '$_POST[cty]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
在这里,让我更正语法:
$sql="INSERT INTO exp VALUES('".$_POST['fnm']."','".$_POST['lnm']."','".$_POST['mob']."', '".$_POST['age']."','".$_POST['src']."','".$_POST['des']."','".$_POST['str']."','".$_POST['area']."', '".$_POST['cty']."')";
this code is wrong : $sql="INSERT INTO exp VALUES('$_POST[fnm]', '$_POST[lnm]', $_POST[mob], $_POST[age], '$_POST[src]', '$_POST[des]', '$_POST[str]', '$_POST[area]', '$_POST[cty]')";
此代码是错误的: $sql="INSERT INTO exp VALUES('$_POST[fnm]', '$_POST[lnm]', $_POST[mob], $_POST[age], '$_POST[src]', '$_POST[des]', '$_POST[str]', '$_POST[area]', '$_POST[cty]')";
as you are not referring $_POST variables using their name
, missing the quotes surrounding the variable name . 因为您没有$_POST variables using their name
引用 $_POST variables using their name
,所以缺少了变量名周围的引号 。 correct the line and your data should be inserting successfully. 更正该行,您的数据应成功插入。
Try using the following PHP and let us know what happens: 尝试使用以下PHP,让我们知道会发生什么:
$sql="INSERT INTO `exp` VALUES('{$_POST['fnm']}', '{$_POST['lnm']}',
'{$_POST['mob']}', '{$_POST['age']}', '{$_POST['src']}', '{$_POST['des']}',
'{$_POST['str']}', '{$_POST['area']}', '{$_POST['cty']')"
or die('Error: ' . mysqli_error($con));
$output = $con->$sql
echo "1 record added";
mysqli_close($con);
Is anything different this way? 这样有什么不同吗?
Note: To be syntactically proper you would want to explicitly state where you want the values inserted as follows: 注意:为了在语法上正确,您需要明确声明要插入值的位置,如下所示:
$sql="INSERT INTO `table_name` (`first_value`, `second_value`, `third_value`)
VALUES ('{value1}', '{value2}', '{value3}');";
I know it's way too late guys, but I solved the issue. 我知道这太迟了,但是我解决了这个问题。 This doesn't means it took me 3 years to solve it, I just forgot to post the answer. 这并不意味着我花了3年的时间来解决它,我只是忘记发布答案了。 It was happening because I was editing the php code in Wordpad. 发生这种情况是因为我正在Wordpad中编辑php代码。 I copied and saved the code in Notepad and the issue was solved. 我将代码复制并保存在记事本中,此问题已解决。
Thank you for your valuable input guys. 感谢您的宝贵意见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.