与其中的添加不符合预期

这里是我的PHP代码与SQL查询,但输出不是预期的:

$sql = 'INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES '; foreach($all_footers as $key => $val){ $sql .= '('.(int)$data['event_id'].', '.$key + 1 .', '.(int)$val['file_id'].', "'.addslashes($val['url']).'"), '; } $sql = rtrim($sql, ', '); var_dump($sql); exit; 

我得到这样的SQL查询:

 `INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES 1, 2135, "http://11.lt"), 1, 2136, "http://22.lt"), 1, 2140, "http://44.lt")` 

第一个(在VALUES之后?

+. 具有相同的运算符优先级 ,但是是左结合的。 意思是在第一次连接之后:

  '('  (int)$ data ['event_id'] 

string被添加了你的密钥,例如

 "($data['event_id']" + $key 

所以string在这个数字上下文中被转换成一个整数并消失 。 为了解决这个问题,在你的加法周围使用括号()

这是由于运营商的优先权而发生的。 试试 –

 $sql .= '(' . ((int)$data['event_id']) . ', ' . ($key + 1) . ', ' . ((int)$val['file_id']) . ', "' . addslashes($val['url']) . '"), ';