简体   繁体   English

MYSQL-INSERT错误,字段列表中的未知列

[英]MYSQL - INSERT error, unknown column in field list

i keep getting the following error from this simple mysql statement and i cant see why. 我一直从这个简单的mysql语句中得到以下错误,我看不到为什么。 im sure its something obvious. 我肯定它是显而易见的。

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: 数据库表“帐户”包含以下字段:

id, int primary A_I id,int primary 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 ...以该顺序

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 mysqli or PDO to 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. 和其他变量。

Thats because you have syntax error in your INSERT query. 那是因为您在INSERT查询中有语法错误。 String and Date values are to passed into single quotes and not double quotes in sql. 字符串和日期值在sql中传递为单引号而不是双引号 the . or the String concatenation character is also not required. 或不需要字符串串联字符。 So based on the data you provided it might be 因此,根据您提供的数据,可能是

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

Basically what sql is telling you that you are referencing a column in your insert that is not defined in the database. 基本上,sql告诉您的是您正在引用插入中未在数据库中定义的列。 Provide your table structure or ensure that the column name is exactly as you defined in the db. 提供表结构或确保列名与您在数据库中定义的名称完全相同。 HTH. HTH。

You have missed single inverted commas enclosing $notes and $category I guess. 我想您已经错过了用单引号将$ notes和$ category括起来的逗号。 Enclose them in ' and your problem should be solved. 将它们括在'中,您的问题应得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM