简体   繁体   English

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

[英]Error inserting data into mySQL database

My PHP code for the form: 我的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> 
My PHP code for inserting data: 我的用于插入数据的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('Ymd 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); ?> 

Error: Error submitting data:You have an error in your SQL syntax; 错误:提交数据时出错:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''contact us' (name,eadd,subject,description,date) VALUES ('Sahil Sunny','sahils' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“与我们联系”(名称,添加,主题,描述,日期)附近使用值(第1行的“ Sahil Sunny”,“ sahils”

You need to use backticks on tables instead of quotes. 您需要在表上使用反引号而不是引号。 The identifier quote character is the backtick. 标识符引号是反引号。

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

Sidenote: I suggest since you are using mysqli, utilize prepared statements to execute safer queries. 旁注:我建议,因为您使用的是mysqli,请利用准备好的语句执行更安全的查询。

Use `contact us` instead of 'contact us' . 使用“联系我们”而不是“联系我们”。

One more thing is that avoid space in table name. 还有一件事是避免表名中有空格。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM