我有以下内容:-
$pdo = new SQL();
$dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);
if ($fieldBookingdate == NULL) {$fieldBookingdate = 'bookingdate';}
if ($fieldReturndate == NULL) {$fieldReturndate = 'returndate';}
if ($fieldBookingtime == NULL) {$fieldBookingtime = 'bookingtime';}
if ($fieldReturntime == NULL) {$fieldReturntime = 'returntime';}
if ($fieldPassengername == NULL) {$fieldPassengername = 'passengername';}
if ($fieldPassengeremail == NULL) {$fieldPassengeremail = 'passengeremail';}
if ($fieldPaddress == NULL) {$fieldPaddress = 'paddress';}
if ($fieldVaddress == NULL) {$fieldVaddress = 'vaddress';}
if ($fieldDaddress == NULL) {$fieldDaddress = 'daddress';}
if ($fieldCartype == NULL) {$fieldCartype = 'cartype';}
if ($fieldFlightnumber == NULL) {$fieldFlightnumber = 'flightnumber';}
if ($fieldComments == NULL) {$fieldComments = 'comments';}
$this->sql = "UPDATE tblbookings SET bookingdate = '$fieldBookingdate', returndate = '$fieldReturndate', bookingtime = '$fieldBookingtime'
returntime = '$fieldReturntime', passengername = '$fieldPassengername', passengeremail = '$fieldPassengeremail', paddress = '$fieldPaddress'
vaddress = '$fieldVaddress', daddress = '$fieldDaddress', cartype = '$fieldCartype', flightnumber = '$fieldFlightnumber', comments = '$fieldComments' WHERE
bookref = '$fieldBookingreference';";
基本上UPDATE
将修改12个数据库字段,但可能只设置了11个值。(MySQL)
对于没有设置的值,我知道你可以像UPDATE tblbookings SET bookingdate = bookingdate
一样更新,它会工作。但是,因为我在查询中有引号内的这些值,所以它正在更新为UPDATE tblbookings SET bookingdate = 'bookingdate'
-并且失败。
我怎样才能使它工作?
更新1:例子;
UPDATE tblbookings SET bookingdate = 'bookingdate' WHERE
bookref = 'BR1360'
我得到以下错误:-
1292 -第133行'bookingdate'列的'bookingdate'日期值不正确
如果我这样做:-
UPDATE tblbookings SET bookingdate = bookingdate WHERE
bookref = 'BR1360'
这将工作,并保持bookingdate值不变
查询中缺少逗号导致的错误
bookingtime = '$fieldBookingtime' returntime = '$fieldReturntime',
//^ here the comma is missing
所以作曲家试图将bookingtime
列设置为'fieldBookingtime' returntime...
,就像你传递的字符没有被引号转义。
正如我已经在我的评论中解释的那样,查询没有失败,因为您使用了您认为的引号。
设法使其工作如下:-
if ($fieldBookingdate == NULL) {$fieldBookingdate = "bookingdate = bookingdate";} else {$fieldBookingdate = "bookingdate = '$fieldBookingdate'";}
因此,如果值没有设置,它将保留字段为(bookingdate = bookingdate
),对于已设置的值,它将更新为($fieldBookingdate = "bookingdate = '$fieldBookingdate'";
)