php MYSQL - 插入错误,字段列表中的未知列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15710397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:48:16  来源:igfitidea点击:

MYSQL - INSERT error, unknown column in field list

phpmysqlsql-insert

提问by Lee Ginger-Ninja McCarthy

i keep getting the following error from this simple mysql statement and i cant see why. im sure its something obvious.

我不断从这个简单的 mysql 语句中收到以下错误,我不明白为什么。我确定它的东西很明显。

require_once("connect.php");

$query = mysql_query("SELECT * FROM accounts ORDER BY id DESC LIMIT 1");
$row = mysql_fetch_assoc($query);

$balanceold = $row['balance'];
$difference = $_POST['predec'].".".$_POST['dec'];

$category = $_POST['category'];
$notes = $_POST['notes'];

if(isset($_POST['in'])){
$balancenew = $balanceold + $difference;
$query = mysql_query("INSERT INTO accounts(currentbalance, balancein, category, notes) VALUES (".$balancenew.", ".$difference.", ".$category.", ".$notes.")");  
if($query){
header("Location: budget.php"); 
}
else{
die(mysql_error());
}
}

gives error: Unknown column 'payday' in 'field list'

给出错误:“字段列表”中的未知列“发薪日”

here is my form code:

这是我的表单代码:

<form action=process.php method=post>

&pound;
<input type=text name=predec size=7>
. 
<input type=text name=dec size=4 value=00>
<br />
<select name=category>
<option value=payday>Payday</option>
</select>
<input type=text name=notes size=20>
<input type=submit name=in value=Deposit>
<input type=submit name=out value=Withdraw>
</form> 

database table"accounts" contains the following fields:

数据库表“accounts”包含以下字段:

id, int primary A_I

id, int 主要 A_I

balancein, decimal 10,2

平衡,十进制 10,2

balanceout, decimal 10,2

平衡,十进制 10,2

current balance, decimal 10,2

当前余额,十进制 10,2

category, varchar 50

类别,varchar 50

notes, varchar 255

注释,varchar 255

date, timestamp

日期、时间戳

...in that order

...以该顺序

回答by

try this (enclose each variable inside query with single quota):

试试这个(用单个配额将查询中的每个变量括起来):

mysql_query("INSERT INTO accounts(currentbalance, balancein, category, notes) 
          VALUES ('$balancenew', '$difference', '$category', '$notes')");  

Its better to use mysqlior PDOto prevent from SQL injection attack, you could use mysql_real_escape_string() for now:

最好使用mysqliPDO来防止 SQL 注入攻击,您现在可以使用 mysql_real_escape_string():

$balancenew = mysql_real_escape_string($balancenew);

and for other variables.

和其他变量。

回答by Azan Momin

Thats because you have syntax error in your INSERT query. String and Date values are to passed into single quotesand not double quotesin sql. the .or the String concatenation character is also not required. So based on the data you provided it might be

那是因为您的 INSERT 查询中有语法错误。字符串和日期值在 sql 中传递到单引号而不是双引号中。的或者也不需要字符串连接字符。因此,根据您提供的数据,它可能是

$query = mysql_query("INSERT INTO accounts(currentbalance, balancein, category, notes) 
                      VALUES ($balancenew, $difference, '$category', '$notes')");  

回答by gridrivero

You have missed single inverted commas enclosing $notes and $category I guess. Enclose them in ' and your problem should be solved.

我猜你错过了包含 $notes 和 $category 的单引号。将它们括在 ' 中,您的问题应该得到解决。

回答by John Brown

Basically what sql is telling you that you are referencing a column in your insert that is not defined in the database. Provide your table structure or ensure that the column name is exactly as you defined in the db. HTH.

基本上什么 sql 告诉您您正在引用插入中未在数据库中定义的列。提供您的表结构或确保列名与您在数据库中定义的完全相同。哈。