I've used this exact code with a slight modification of the values in another script and it worked just fine, but this time it's giving me errors. This is the line:
我使用了这段代码,对另一个脚本中的值做了一点修改,效果很好,但是这次它给我带来了错误。这是线:
$result = mysql_query("INSERT INTO contacts (name, email, telephone, companyname, postcode, message, date) VALUES('" . $name . "', '" . $userEmail . "', '" . $telephone . "', '" . $companyName . "', '" . $message . "', '" . $date . "'") or die (mysql_error());
I've tried echo-ing out all of the variables with this:
我试着用这个来重复所有的变量:
<p>Name: <?php echo $name; ?>, <br />Email: <?php echo $userEmail; ?>, <br />Telephone: <?php echo $telephone; ?>, <br />Company name: <?php echo $companyName; ?>, <br />Message: <?php echo $message; ?>, <br />Date: <?php echo $date; ?>, <br /></p>
Which displays them all fine when the form is submitted. However, when I try to add them to a database it flips out and says this:
当表单提交时,显示它们都很好。然而,当我试图将它们添加到数据库中时,它会跳出来,并说:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
您的SQL语法有错误;请检查与MySQL服务器版本对应的手册,以获得在“第1行”附近使用的正确语法
I'm assuming I'm either missing something simple, I've made a typo somewhere (although I've re-typed it to double check it wasn't that).
我假设我错过了一些简单的东西,我在某个地方打了个错码(尽管我重新打了一遍以确认它不是那个)。
Here's the way my database is set-up:
我的数据库是这样建立的:
Any ideas?
什么好主意吗?
UPDATE
更新
I added the missing )
to the query and that got rid of the first error, but now I'm getting this error when I submit it:
我将缺失的部分添加到查询中,这样就消除了第一个错误,但是现在我在提交时得到了这个错误:
Column count doesn't match value count at row 1
列计数与第1行的值计数不匹配。
FIXED
固定
I was missing the $postcode
variable. Doh!
我缺少$postcode变量。哎!
2 个解决方案
#1
2
You are missing a )
at the end of you query, for the VALUES()
.
在查询结束时,您将丢失a)作为值()。
$result = mysql_query("INSERT INTO contacts (name, email, telephone, companyname, postcode, message, date) VALUES('" . $name . "', '" . $userEmail . "', '" . $telephone . "', '" . $companyName . "', '" . $message . "', '" . $date . "')") or die (mysql_error());
#2
1
You declare one more field than give in VALUES. This might be the thing with column count.
声明的字段比给出的值多一个。这可能是列计数。
#1
2
You are missing a )
at the end of you query, for the VALUES()
.
在查询结束时,您将丢失a)作为值()。
$result = mysql_query("INSERT INTO contacts (name, email, telephone, companyname, postcode, message, date) VALUES('" . $name . "', '" . $userEmail . "', '" . $telephone . "', '" . $companyName . "', '" . $message . "', '" . $date . "')") or die (mysql_error());
#2
1
You declare one more field than give in VALUES. This might be the thing with column count.
声明的字段比给出的值多一个。这可能是列计数。