Dan
Dan

Reputation: 5

Mulitple values in PHP Prepared Statements

Using prepared statements, I've got 23 values that are going to be entered (see below). Does this mean I need to type out 23 ? placements or is there some kind of default / shorthand?

INSERT INTO table 
            (
            job_id, property_title, property_location, property_price, number_of_bedrooms, 
            number_of_receptions, number_of_bathrooms, epc, train_station_miles, 
            garden_acres, garage, off_road_parking, main_photo, 
            photo_1, photo_2, return_email, office, 
            additional_information, timestamp_added, added_by_user_id, timestamp_updated, 
            updated_by_user_id, status_id
            ) 
            VALUES (?, ?, ?, ?,......etc x 23)

I've never thought about this before as I've only used a smaller number of values but 23 seems a bit excessive if there is some kind of shorthand

Upvotes: 0

Views: 60

Answers (1)

MinistryOfChaps
MinistryOfChaps

Reputation: 1484

This should be suitable for you, to shorthand binding all of your inputs into the query I'd recommend this code:

$placeholders = implode(', ', array_fill(0, 23, '?'));

$stmt = $connection->prepare("INSERT INTO table 
            (
           ... rest of the statement....
            ) 
            VALUES ($placeholders)");

Upvotes: 2

Related Questions