我需要快速查看我的SQL查询。它已经让我头疼。比较我得到的一些建议,我无法弄清楚我的SQL的正确格式。
这是我的代码:
$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());
提前致谢!
修改
目前我将此作为表格:
<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>
这是插入查询:
<?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>";
?>
似乎提交了条目,但没有提供价值......我厌倦了看它而没有看到它。我感谢所有的帮助。谢谢!
编辑(重播):
正如Salman A所指出的那样,问题实际上是形式,以及每个输入都是用“id”而不是“名称”来识别的事实。
谢谢大家,非常感谢所有的帮助!我想我不正确的SQL格式是另一个主题,嗯?
答案 0 :(得分:2)
NULL
不应该被引用。
您可以简化值列表(因为这似乎是PHP):
VALUES (NULL , '$first', '$last', '$add1', '$add2', '$phone_home', '$phone_cell', '$phone_dad', '$phone_mom', '$email1', '$email2')";
答案 1 :(得分:2)
闻起来的形式!全部改变:
<input id="yadayada">
要:
<input name="yadayada">
<!-- ^^^^---- you must use the name attribute -->
可选:
<table>
放在<form>
内,而不是相反。</form>
和<input type="submit">
。multipart/form-data
编码。答案 2 :(得分:0)
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."')";
答案 3 :(得分:0)
在字符串中加一个分号。
您从数据库获得的错误是什么(查看mysql日志)