我一直这样 - “字段列表”中的未知列'rec_label'

时间:2010-09-30 17:26:22

标签: php mysql

这是我的代码

<?
if ((!$_POST['id']) || (!$_POST['format']) || (!$_POST['title'])) {
header ("Location: /show_addrecord.html");
exit;
}
$db_name = "testdb2";
$table_name = "my_music";
$connection = @mysql_connect ("localhost", "spike", "9sj7En4")
or die (mysql_error());
$db = @mysql_select_db ($db_name, $connection) or die(mysql_error());

//create SQL statement and issue query
$id = mysql_escape_string($_POST['id']);
$format = mysql_escape_string($_POST['format']);
$title = mysql_escape_string($_POST['title']); 
$artist_fn = mysql_escape_string($_POST['artist_fn']);
$artist_ln = mysql_escape_string($_POST['artist_ln']);
$rec_label = mysql_escape_string($_POST['rec_label']);
$my_notes = mysql_escape_string($_POST['my_notes']);
$date_acq = mysql_escape_string($_POST['date_acq']);

$sql = "INSERT INTO $table_name
(id, format, title, artist_fn, artist_ln, rec_label, my_notes, date_acq) VALUES
('$_POST[id]',
'$_POST[format]',
'$_POST[title]',
'$_POST[rec_label]', 
'$_POST[artist_fn]',
'$_POST[artist_ln]',
'$_POST[my_notes]',
'$_POST[date_acq]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>

<html>

<head>
<title>Add a Record</title>

</head>

<body>
<table cellspacing=3 cellpadding=3>
<tr>
<td valign=top>
<p><strong>ID:</strong><br>
<? echo "$_POST[id]"; ?></p>
</td>

<td>
<p><strong>Date Acquired (YYYY-MM-DD):</strong><br>
<? echo "$_POST[date_acq]"; ?></p>
</td>

<td valign=top>
<p><strong>Format:</strong><br>
<? echo "$_POST[format]"; ?>
</p>
</td>
</tr>

<tr>
<td valign=top>
<p><strong>Title:</strong><br>
<? echo "$_POST[title]"; ?></p>
</td>

<td valign=top>
<p><strong>Record Label</strong><br>
<? echo "$_POST[rec_label]"; ?></p>
</td>
</tr>

<td valign=top>
<p><strong>Artist's First Name:</strong><br>
<? echo "$_POST[artist_fn]"; ?>
</p>
</td>

<td valign=top>
<p><strong>Artist's Last Name:</strong><br>
<? echo "$_POST[artist_ln]"; ?></p>
</td>
</tr>

<tr>
<td valign=top colspan=2 align=center>
<p><strong>My Notes:</strong><br>
<? echo stripslashes($_POST[my_notes]); ?></p>

<p><a href="show_addrecord.html">Add Another</a></p>
</td>
</tr>
</table>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

最有可能的是,在字段列表中或数据库中列的名称中存在拼写错误。或者,希望列不太可能,该列实际上不存在。

仔细检查您的数据库表。

然而,尽管不太可能,但似乎在这里报告了一个相关的错误(http://bugs.mysql.com/bug.php?id=1689),对于MySQL版本4.0.16我怀疑这会影响你,但值得研究。

答案 1 :(得分:1)

请。使用mysqliprepared statements

<?
if ((!$_POST['id']) || (!$_POST['format']) || (!$_POST['title'])) {
  header ("Location: /show_addrecord.html");
  exit;
}

$db_name = "testdb2";
$table_name = "my_music";
$connection = @mysqli_connect("localhost", "spike", "9sj7En4") or die (mysqli_error());
$db = @mysqli_select_db($connection, $db_name) or die(mysqli_error());

// BTW, using $tablename here is also wrong on several levels. Avoid.
$sql = "
  INSERT INTO $table_name
  (id, format, title, artist_fn, artist_ln, rec_label, my_notes, date_acq) VALUES
  (?,?,?,?,?,?,?,?)
";

$stmt = mysqli_prepare($connection, $sql);

mysqli_stmt_bind_param($stmt, "ssssssss", 
  $_POST['id'],
  $_POST['format'],
  $_POST['title']),
  $_POST['artist_fn'],
  $_POST['artist_ln'],
  $_POST['rec_label']),
  $_POST['my_notes'],
  $_POST['date_acq']
);

$result = @mysqli_stmt_execute($connection, $sql) or die(mysqli_error());

mysqli_stmt_close($stmt);

?>