Protect custom form from SQL injection

Yes, $wpdb->update is sufficient protection. You should not escape or prepare the data. From the documentation of wpdb: data (array) Data to update (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped). This means that if you are using GET or POST data you may … Read more

Creates only one table and not the other

dbdelta demands that: You must put each field on its own line in your SQL statement. You must have two spaces between the words PRIMARY KEY and the definition of your primary key. You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY. You must … Read more

Plugin with connection to database – Single function

Use a global. So, your plugin file would look like this: $myConn = new wpdb( ‘username’, ‘password’, ‘database’, ‘localhost’ ); function plugin_step_1( $arg1, $arg2 ) { global $myConn; //code to do stuff here } function plugin_step_2() { global $myConn; // more code here } function plugin_step_3( $arg1 ) { //I don’t need the wpdb object … Read more

Custom database query to validate data

Assuming that all of your sql column names are correct, something like this should work: if(isset($_POST[‘submit’])){ global $wpdb; $tablename= $wpdb . ‘form_subscribe’; $myrows = $wpdb->get_var( $wpdb->prepare(“SELECT email FROM $tablename WHERE email=%s LIMIT 1″, $_POST[’email’])); if(empty($myrows)){ $data=array( ‘name’ => $_POST[‘fullname’], ‘age’ => $_POST[‘age’], ’email’ => $_POST[’email’] ); $wpdb->insert( $tablename, $data); } else { $status=”User already subscribed”; … Read more

How to set up prepared query using IN statement

My solution to the problem: $prepare = array(); $in = implode(‘,’, array_fill(0, count($product_ids), ‘%d’)); foreach ($product_ids as $ids){ $prepare[] = $ids; } $prepare[] = “post”; $prepare[] = $num; $results = $this->db->get_results($this->db->prepare(“SELECT ID, post_title FROM {$this->db->posts} WHERE ID NOT IN({$in}) AND post_type=%s ORDER BY ID DESC LIMIT %d”, $prepare));

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)