将数据插入mySQL数据库时出错

时间:2014-09-21 08:13:29

标签: php mysql database html-table insert

表单的PHP代码:

body{
	margin:0;
}
#top_nav{
	background-color:#000;
	height:auto;
	max-height:3em;
}
#top_nav a{
	text-decoration:none;
	color:#FFF;
	font-family:Arial, Helvetica, sans-serif;
}
.menu{
	display:inline-block;
	padding:1em;
	font-size:1em;
	height:1em;
}
.menu:hover{
	background-color:#09F;
	border-bottom-left-radius:2em;
	border-bottom-right-radius:2em;
	height:3em;
	-webkit-transition:height 500ms ease;
	-moz-transition:height 500ms ease;
	-ms-transition:height 500ms ease;
	-o-transition:height 500ms ease;
	transition:height 500ms ease;
}
#contact-us{
	color:#09F;
}
#contact-us:hover{
	color:#FFF;
}
#container{
	margin-top:5%;
}
.textbox{
	width:20em;
	border-bottom:.1em solid #000;
	border-top:0;
	border-left:0;
	border-right:0;
	font-family:Arial, Helvetica, sans-serif;
	font-size:2em;
	height:2em;
	border-bottom-left-radius:1em;
	border-bottom-right-radius:1em;
	-webkit-transition:all 500ms ease;
	-moz-transition:all 500ms ease;
	-ms-transition:all 500ms ease;
	-o-transition:all 500ms ease;
	transition:all 500ms ease;
	padding:0 1em 0 1em;
	margin-bottom:2%;
}
.textbox:focus{
	outline:none;
	border-bottom-color:#09F;
	width:30em;
	border-bottom-left-radius:1em;
	border-bottom-right-radius:1em;
}
#textarea{
	height:5em;
	border-top:.1em solid #000;
	border-top-left-radius:1em;
	border-top-right-radius:1em;
	resize:none;
}
#textarea:focus{
	border-top-color:#09F;
}
.submit{
	width:10em;
	border-bottom:.1em solid #09F;
	border-top:.1em solid #09F;
	border-top-left-radius:1em;
	border-top-right-radius:1em;
	border-left:0;
	border-right:0;
	font-family:Arial, Helvetica, sans-serif;
	font-size:2em;
	color:#09F;
	height:2em;
	border-bottom-left-radius:1em;
	border-bottom-right-radius:1em;
	background-color:#000000;
	-webkit-transition:all 500ms ease;
	-moz-transition:all 500ms ease;
	-ms-transition:all 500ms ease;
	-o-transition:all 500ms ease;
	transition:all 500ms ease;
}
.submit:hover{
	border-top:.1em solid #000;
	border-bottom:.1em solid #000;
	background-color:#09F;
	color:#000;
	width:20em;
}
td{
	text-align:center;
}
<html>
<head>
<title>Carzpedia | The best car search engine</title>
<link type="text/css" rel="stylesheet" href="css/contact-us.css"/>
<link rel="icon" href="images/favicon/favicon.ico"/>
</head>
<body>
<div id="top_nav"> <a href="index.php">
  <div class="menu">CZP</div>
  </a> <a href="list-of-car-manufacturers.php">
  <div class="menu">LIST OF CAR MANUFACTURERS</div>
  </a> <a href="why-use-carzpedia.php">
  <div class="menu">WHY USE CARZPEDIA?</div>
  </a> <a href="about-us.php">
  <div class="menu">ABOUT US</div>
  </a> <a href="contact-us.php">
  <div class="menu" id="contact-us">CONTACT US</div>
  </a> </div>
<div id="container">
<form method="post" action="php/send-data.php">
<table align="center">
<tr>
<td><input class="textbox" type="text" placeholder="Please enter full name..." name="name"/></td>
</tr>
<tr>
<td><input class="textbox" type="email" placeholder="Please enter email address..." name="eadd"/></td>
</tr>
<tr>
<td><input class="textbox" type="text" placeholder="Subject" name="subject"/></td>
</tr>
<tr>
<td><textarea class="textbox" id="textarea" placeholder="Description (Max 5000 characters)" name="description"></textarea>
</td>
</tr>
<tr>
<td align="center"><input class="submit" type="submit" value="SUBMIT" name="submit"/></td>
</tr>
</table>
</form> 
</div>
</body>
</html>
我的用于插入数据的PHP代码:

<?php
//create connection
$con=mysqli_connect("localhost","root","","carzpedia");

//check connection
if(mysqli_connect_errno())
{
    echo"Failed to connect to MySQL:".mysqli_connect_errno();
}

//get data from form
$name=$_POST['name'];
$eadd=$_POST['eadd'];
$subject=$_POST['subject'];
$description=$_POST['description'];
$date=date('Y-m-d h:i:sa');

//submit data
$sql="INSERT INTO 'contact us' (name,eadd,subject,description,date)
VALUES ('$name','$eadd','$subject','$description','$date')";
if(mysqli_query($con,$sql))
{
    echo "Data submitted successfully";
}
else
{
echo"Error submitting data:".mysqli_error($con);
}

//close connection
mysqli_close($con);
?>

错误: 提交数据时出错:您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在“联系我们”(姓名,eadd,主题,描述,日期)附近使用正确的语法VALUES('Sahil Sunny','sahils'在第1行

2 个答案:

答案 0 :(得分:0)

您需要在表格上使用反引号而不是引号。标识符引号字符是反引号。

INSERT INTO `contact us` (name,eadd,subject,description,date)
            ^          ^ backticks not quotes (')
VALUES ('$name','$eadd','$subject','$description','$date')

旁注:我建议你使用mysqli,利用预准备语句来执行更安全的查询。

答案 1 :(得分:0)

使用“联系我们”而不是“联系我们”#39;

还有一件事是避免表名中的空格。

干杯。