What is the consensus on how to insert variables in a MySQL query using the
I know this hasn't worked:
$insert_row = $wpdb->query($wpdb->prepare("INSERT INTO wp_mytable ( product ) VALUES (%s) ", $product));
Where echoing $product indeed echoes the string I want to insert.
The other syntax that has worked for me in the past (but not via the $wpdb syntax) is:
INSERT INTO wp_mytable ('product') VALUES '%s', $product;
I'm still getting a blank field value in the database with all above methods. The row is created but the value is missing.
$insert_row = $wpdb->insert( 'wp_mytable', array( 'product' => $product, 'votes' => 1), array( '%s', '%d' ) );
This still produces a blank entry under the product column. The vote is counted for the row but the product field is left blank.
This is how I declare $product:
$product = wp_get_object_terms($post->ID,'product'); $product = strval($product->name);
I'm selecting the first object term's name and making sure it's a string.
Also what's interesting about the update query is that although the WHERE clause has its own array, it requires an AND for multiple clauses. This is confusing if you are declaring variables for the WHERE parameters:
$update_vote = $wpdb->update( 'wp_mytable', array( 'votes' => $new_votes ), array( 'product' => $product 'company' => $company), // WHERE DOES 'AND' GO? array( '%d' ), array( '%s', '%s' ) );