在这里,我在mysql插入查询中使用多个数据。我最后使用逗号","
。但是如果我想从插入查询中删除最后一个逗号,我们该怎么做呢?
我的疑问是:
$sql.= INSERT INTO shipping_boxes(ship_method,order_id,box_id,rate,signature,weight,length,width,height,value,markupPercentage,insured) VALUES("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","99999","0","0","21.9","24","24","21","200","","0"),("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","88888","0","0","18","20","20","18","140","","0"),("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","88888","0","0","18","20","20","18","140","","0"),;
更新 我想这样,
$sql.= INSERT INTO shipping_boxes(ship_method,order_id,box_id,rate,signature,weight,length,width,height,value,markupPercentage,insured) VALUES("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","99999","0","0","21.9","24","24","21","200","","0"),("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","88888","0","0","18","20","20","18","140","","0"),("Pickup - Deliver By Messenger: Approximately 1 Business Day, by 5 PM","15566","88888","0","0","18","20","20","18","140","","0");
答案 0 :(得分:3)
我认为你是在循环中创建你的sql字符串:
$sql = "INSERT INTO mytable (col1, col2, col3) VALUES "
for ($i = 1; $i <= 10; $i++) {
$sql .= "('val1', 'val2', 'val3'),"
}
然后你可以用rtim删除最后一个逗号:
rtrim($sql, ",")
但我建议您使用带有占位符的预准备语句。如果您使用的是mysqli,它就是这样的:
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("INSERT INTO mytable (col1, col2, col3) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $value1, $value2, $value3);
for (....) {
$value1 = 'something';
$value2 = 'something';
$value3 = 'something';
$stmt->execute();
}
$stmt->close();
$conn->close();