6

我从这个简单的 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());
}
}

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

这是我的表单代码:

<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> 

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

id, int 主要 A_I

balancein,十进制 10,2

余额,十进制 10,2

当前余额,十进制 10,2

类别,varchar 50

注释,varchar 255

日期,时间戳

...以该顺序

4

4 回答 4

14

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

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:

$balancenew = mysql_real_escape_string($balancenew);

and for other variables.

于 2013-03-29T19:48:19.790 回答
1

Thats because you have syntax error in your INSERT query. String and Date values are to passed into single quotes and not double quotes in 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')");  
于 2014-04-16T13:20:00.663 回答
0

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

于 2013-03-29T19:47:12.657 回答
0

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

于 2014-04-16T13:23:25.653 回答