Reputation: 357
Here, I am using multiple data in mysql insert query. I am using comma ","
in last. But If I want to remove last comma from insert query, how can we do that?
My query is:
$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"),;
Updated: I want like this,
$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");
Upvotes: 0
Views: 2072
Reputation: 49059
I think you are creating your sql string in a loop:
$sql = "INSERT INTO mytable (col1, col2, col3) VALUES "
for ($i = 1; $i <= 10; $i++) {
$sql .= "('val1', 'val2', 'val3'),"
}
then you can just remove the last comma with rtim:
rtrim($sql, ",")
but I would suggest you to use a prepared statement with placeholders instead. If you are using mysqli it's something like this:
$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();
Upvotes: 3