2014-09-21 112 views
1

我对形式的PHP代码:错误插入数据到MySQL数据库

body{ 
 
\t margin:0; 
 
} 
 
#top_nav{ 
 
\t background-color:#000; 
 
\t height:auto; 
 
\t max-height:3em; 
 
} 
 
#top_nav a{ 
 
\t text-decoration:none; 
 
\t color:#FFF; 
 
\t font-family:Arial, Helvetica, sans-serif; 
 
} 
 
.menu{ 
 
\t display:inline-block; 
 
\t padding:1em; 
 
\t font-size:1em; 
 
\t height:1em; 
 
} 
 
.menu:hover{ 
 
\t background-color:#09F; 
 
\t border-bottom-left-radius:2em; 
 
\t border-bottom-right-radius:2em; 
 
\t height:3em; 
 
\t -webkit-transition:height 500ms ease; 
 
\t -moz-transition:height 500ms ease; 
 
\t -ms-transition:height 500ms ease; 
 
\t -o-transition:height 500ms ease; 
 
\t transition:height 500ms ease; 
 
} 
 
#contact-us{ 
 
\t color:#09F; 
 
} 
 
#contact-us:hover{ 
 
\t color:#FFF; 
 
} 
 
#container{ 
 
\t margin-top:5%; 
 
} 
 
.textbox{ 
 
\t width:20em; 
 
\t border-bottom:.1em solid #000; 
 
\t border-top:0; 
 
\t border-left:0; 
 
\t border-right:0; 
 
\t font-family:Arial, Helvetica, sans-serif; 
 
\t font-size:2em; 
 
\t height:2em; 
 
\t border-bottom-left-radius:1em; 
 
\t border-bottom-right-radius:1em; 
 
\t -webkit-transition:all 500ms ease; 
 
\t -moz-transition:all 500ms ease; 
 
\t -ms-transition:all 500ms ease; 
 
\t -o-transition:all 500ms ease; 
 
\t transition:all 500ms ease; 
 
\t padding:0 1em 0 1em; 
 
\t margin-bottom:2%; 
 
} 
 
.textbox:focus{ 
 
\t outline:none; 
 
\t border-bottom-color:#09F; 
 
\t width:30em; 
 
\t border-bottom-left-radius:1em; 
 
\t border-bottom-right-radius:1em; 
 
} 
 
#textarea{ 
 
\t height:5em; 
 
\t border-top:.1em solid #000; 
 
\t border-top-left-radius:1em; 
 
\t border-top-right-radius:1em; 
 
\t resize:none; 
 
} 
 
#textarea:focus{ 
 
\t border-top-color:#09F; 
 
} 
 
.submit{ 
 
\t width:10em; 
 
\t border-bottom:.1em solid #09F; 
 
\t border-top:.1em solid #09F; 
 
\t border-top-left-radius:1em; 
 
\t border-top-right-radius:1em; 
 
\t border-left:0; 
 
\t border-right:0; 
 
\t font-family:Arial, Helvetica, sans-serif; 
 
\t font-size:2em; 
 
\t color:#09F; 
 
\t height:2em; 
 
\t border-bottom-left-radius:1em; 
 
\t border-bottom-right-radius:1em; 
 
\t background-color:#000000; 
 
\t -webkit-transition:all 500ms ease; 
 
\t -moz-transition:all 500ms ease; 
 
\t -ms-transition:all 500ms ease; 
 
\t -o-transition:all 500ms ease; 
 
\t transition:all 500ms ease; 
 
} 
 
.submit:hover{ 
 
\t border-top:.1em solid #000; 
 
\t border-bottom:.1em solid #000; 
 
\t background-color:#09F; 
 
\t color:#000; 
 
\t width:20em; 
 
} 
 
td{ 
 
\t 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'at line 1

+0

为什么你的表有一个空格分隔符,它会在进一步导致你很多问题 – 2014-09-21 08:24:54

+0

那么我有点新的PHP,所以我可能知道什么是空格分隔符:)? – VOXRAZR 2014-09-21 08:59:40

回答

0

你需要在表上,而不是引号使用反引号的标识符引号字符是反引号

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

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

+1

@OP - 你可以直接对表格进行处理,而不需要任何特别的引用或反驳。多数民众赞成我通常这样做(http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks)。此外,不是问题的一部分,但是在将输入放入查询之前(您需要验证的准备好的语句)之前,您应该验证并清理输入。 – 2014-09-21 08:36:04

+0

Thanx男人你救了我的一天,关于验证我只是测试提交,我会稍后添加:) – VOXRAZR 2014-09-21 08:58:34

+0

@VOXRAZR确实很高兴它洒了一些光 – Ghost 2014-09-21 09:00:21

0

使用`。联系我们'而不是'联系我们'。

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

干杯。