Are my $wpdb queries efficient?
-
I have a company post type and a rounds table. The rounds table contains data about how much money each company raised and in how many rounds.
The user can basically add and remove how many rounds that he want.
A round looks like this –
Round – 1, Amount – $100,000, date-‘2016-08-31’.When the user hits save/update on the post I first delete all of the rounds and then add them again. Since I don’t know if this is an update or an insert.
Besides security can you please tell me if I’m doing it right? Is there a way maybe to insert them all in one query?
I’m just used to ORM’s and this is my first time writing plain SQL queries.Here is the code:
global $wpdb; $amount_raised = $_POST["amount_raised"]; $round_number = $_POST["round_number"]; $raised_date = $_POST["raised_date"]; //I can pick either field and the count will always be the same. $count = count( $amount_raised ); $table_name = $wpdb->prefix . 'rounds'; $wpdb->query( "DELETE FROM $table_name WHERE post_id = $post_id" ); for ( $i = 0; $i < $count; $i++ ) { $wpdb->query( " INSERT INTO $table_name (post_id, amount_raised, round_number, raised_date) VALUES ('$post_id', '$amount_raised[$i]', '$round_number[$i]', '$raised_date[$i]') " ); }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Are my $wpdb queries efficient?’ is closed to new replies.