[英]Why isn't this MySQL statement working?
I have a table Seats
with the columns, SeatID
, Date
, RouteID
and Seats
. 我有一个带有列的表
Seats
,即SeatID
, Date
, RouteID
和Seats
。 The primary key is SeatsID
. 主键是
SeatsID
。 The value of seats is 50, and I wish to subtract whatever the user has entered into $tickettotal
from 50 and then insert the new value. 席位的值为50,我希望从50中减去用户在
$tickettotal
输入的任何值,然后插入新值。
The user also has to enter which RouteID
and Date
they wish to travel. 用户还必须输入他们希望旅行的
RouteID
和Date
。 I want these to be inserted into the Seats
table along with the new value of Seats
. 我希望将这些值与
Seats
的新值一起插入Seats
表中。
If another user enters the same Date
and RouteID
then the Seats
should be updated. 如果另一个用户输入相同的
Date
和RouteID
则应该更新Seats
。 Otherwise a new record should be inserted with SeatID
, Date
, RouteID
, and the reduced value for Seats
. 否则,应插入一个新记录,其中包含
SeatID
, Date
, RouteID
以及Seats
的折算值。
I thought this statement should do it but I keep getting errors back such as 我以为该语句应该执行此操作,但我不断收到错误提示,例如
Can't find string terminator '"' anywhere before EOF, although when i put " at the end of the statement i just get another syntax error line 108, near ""') ON DUPLICATE KEY UPDATE Seats=Seats-'"$tickettotal" .
Update 更新
I now have it inserting fine, although on a duplicate entry of both Date
and RouteID
it doesn't update Seats
: it still just inserts a new value. 我现在让它插入很好,尽管在
Date
和RouteID
的重复条目上它不会更新Seats
:它仍然只是插入一个新值。 The Seats - $tickettotal
isn't working either. Seats - $tickettotal
都不起作用。
$dbh->do("INSERT INTO $Stable(Date,RouteID)
VALUES ('$Tdate','$Rid') ON DUPLICATE KEY UPDATE Seats=Seats-'$tickettotal'");
Update Answer 更新答案
Because i didn't have a unique column, I created one using both date and RouteID added together so. 因为我没有唯一的列,所以我同时使用date和RouteID创建了一个列。
$Tdate =~ s/-//gi;
$dateandrid = $Tdate . $Rid;
The first line removes the hyphens and the second puts them together. 第一行删除连字符,第二行将它们放在一起。 Then using these statements get the desired effect i wanted.
然后使用这些语句获得我想要的预期效果。
$dbh->do("INSERT INTO $Stable(RouteAndDate,Date,RouteID) VALUES ('$dateandrid','$Tdate','$Rid') ON DUPLICATE KEY UPDATE Seats=Seats");
$dbh->do("INSERT INTO $Stable(RouteAndDate,Date,RouteID) VALUES ('$dateandrid','$Tdate','$Rid') ON DUPLICATE KEY UPDATE Seats=Seats-'$tickettotal'");
You must avoid SQL Injection with parameter binding: 您必须避免通过参数绑定进行SQL注入:
$dbh->do( qq{
INSERT INTO $Stable(Date, RouteID)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE Seats=Seats-?
},
undef,
$Tdate, $Rid, $tickettotal
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.