[英]What's wrong with my SQL query?
I need a quick look at my SQL query.我需要快速查看一下我的 SQL 查询。 It's already given me a headache.
已经让我头疼了。 Comparing some advice I have gotten, I could not figure out the proper format for my SQL.
比较我得到的一些建议,我无法为我的 SQL 找出正确的格式。
This is my code:这是我的代码:
$query = "INSERT INTO `[my_db_name]`.`members` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
VALUES ('NULL' , '".$first."', '".$last."', '".$add1."', '".$add2."', '".$phone_home."', '".$phone_cell."', '".$phone_dad."', '".$phone_mom."', '".$email1."', '".$email2."')";
`mysql_query($query) or die ('Error updating database, Error:' . mysql_error());
Thanks in advance!提前致谢!
EDIT:编辑:
Currently I have this as the form:目前我有这个作为形式:
<table border="0" cellpadding="0" cellspacing="0">
<form enctype="multipart/form-data" method="POST" action="add-member.php">
<tr class="labels">
<td class="f">Add a Member</td>
<td class="l"></td>
</tr>
<tr>
<td class="f"><label for="first">First Name:</label></td>
<td class="l"><input id="first" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="last">Last Name:</label></td>
<td class="l"><input id="last" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="add1">Address 1:</label></td>
<td class="l"><input id="add1" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="add2">Address 2:</label></td>
<td class="l"><input id="add2" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="phone_home">Home Phone:</label></td>
<td class="l"><input id="phone_home" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="phone_cell">Cell Phone:</label></td>
<td class="l"><input id="phone_cell" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="phone_dad">Dad Cell:</label></td>
<td class="l"><input id="phone_dad" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="phone_mom">Mom Cell:</label></td>
<td class="l"><input id="phone_mom" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="email1">Email 1:</label></td>
<td class="l"><input id="email1" type="text"/></td>
</tr>
<tr>
<td class="f"><label for="email2">Email 2:</label></td>
<td class="l"><input id="email2" type="text"/></td>
</tr>
</table>
This is the insert query:这是插入查询:
<?php
$first = $_POST['first'];
$last = $_POST['last'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$phone_home = $_POST['phone_home'];
$phone_cell = $_POST['phone_cell'];
$phone_dad = $_POST['phone_dad'];
$phone_mom = $_POST['phone_mom'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
include ("../lib/login.php");
mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
mysql_select_db ([dbname]);
$query = "INSERT INTO `[dbname]`.`mem_dir` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
VALUES (NULL , '$first', '$last', '$add1', '$add2', '$phone_home', '$phone_cell', '$phone_dad', '$phone_mom', '$email1', '$email2')";
mysql_query($query) or die ('Error updating database, Error:' . mysql_error());
echo "Database successfully uploaded with:<br/><ul>
<li>" . $first . "</li>
...
</ul>";
?>
Seems to submit the entry, but not the values... I'm tired of looking at it and not seeing it.似乎提交了条目,但没有提交值......我厌倦了看着它却没有看到它。 I appreciate all the help.
我感谢所有的帮助。 Thanks!
谢谢!
EDIT (AGAIN):编辑(再次):
As was pointed out by Salman A , the problem was in fact the form, and the fact that each input was identified with an "id", instead of a "name".正如Salman A所指出的,问题实际上是形式,以及每个输入都用“id”而不是“name”标识的事实。
Thanks everyone, I really appreciate all the help!谢谢大家,我真的很感谢所有的帮助! I guess my incorrect SQL formatting is another topic, eh?
我想我不正确的 SQL 格式是另一个话题,嗯?
NULL
should not be quoted. 不应引用
NULL
。
You can simplify the values list (since this seems to be PHP): 您可以简化值列表(因为这似乎是PHP):
VALUES (NULL , '$first', '$last', '$add1', '$add2', '$phone_home', '$phone_cell', '$phone_dad', '$phone_mom', '$email1', '$email2')";
Its the form that smells! 它的形状闻起来! Change all:
改变所有:
<input id="yadayada">
To: 至:
<input name="yadayada">
<!-- ^^^^---- you must use the name attribute -->
Optional: 可选的:
<table>
inside the <form>
, not the other way round. <table>
放在<form>
,而不是反过来。 </form>
and <input type="submit">
. </form>
和<input type="submit">
。 multipart/form-data
encoding unless you're uploading files. multipart/form-data
编码。 NULL values aren't escaped, that's the problem (in case the relative SQL value is really NULL, not a string "NULL") NULL值不会被转义,这就是问题(如果相对SQL值真的是NULL,而不是字符串“NULL”)
$query = "INSERT INTO `[my_db_name]`.`members` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
VALUES (NULL , '".$first."', '".$last."', '".$add1."', '".$add2."', '".$phone_home."', '".$phone_cell."', '".$phone_dad."', '".$phone_mom."', '".$email1."', '".$email2."')";
Put a semicolon inside your string as well. 在你的字符串中也加一个分号。
What's the error you're getting from the database (look in mysql logs) 你从数据库得到的错误是什么(查看mysql日志)
Are you planning to enter the string 'NULL' or value of NULL?您打算输入字符串 'NULL' 或 NULL 值吗? If it is value then it should not be in quotes.
如果它是值,那么它不应该用引号引起来。 I think that could be the issue
我认为这可能是问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.