【问题标题】:Error inserting data into mySQL database将数据插入 mySQL 数据库时出错
【发布时间】:2014-11-15 10:09:39
【问题描述】:

我的表单 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(第 1 行的“Sahil Sunny”、“sahils”

【问题讨论】:

  • 为什么你的表有空格分隔符,以后会出很多问题
  • 我对 php 有点陌生,所以我可以知道什么是空格分隔符 :)?

标签: php mysql database html-table insert


【解决方案1】:

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

插入“联系我们”(姓名、eadd、主题、描述、日期) ^ ^ 反引号不是引号 (') 值('$name','$eadd','$subject','$description','$date')

旁注:由于您使用的是 mysqli,我建议您使用准备好的语句来执行更安全的查询。

【讨论】:

  • @OP - 或者您可以只处理表格而无需任何特定的引用或反引号。这就是我通常的做法(stackoverflow.com/questions/11321491/…)。此外,这不是问题的一部分,但您应该在将输入放入查询之前验证和清理输入(您只需要验证准备好的语句)。
  • 谢谢你拯救了我的一天,关于验证我只是在测试提交,我稍后会添加它:)
  • @VOXRAZR 很高兴它揭示了一些亮点
【解决方案2】:

使用“联系我们”而不是“联系我们”。

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

干杯。

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多