First wpdb insert to mySql new table isn't wor

2019-09-17 05:07发布

I created a new mySql table and I need the first field to be an index and a key.
I'm not sure I got the terminology right but I simply need that field to automatically increment by 1 with each insert.
So I defined that field as an index and gave it the auto_increment attribute.
Now I try to insert the first row like this:

$wpdb->insert('wp_branches', array(user_id=>$user_id, branchName=>$bname));

The index/key field branchId is missing from this query because I'm counting on the db to automatically give it the value 1 since it's the first insert, and then increment it with every additional insert.
For some reason the row isn't being inserted and db is left empty.
What am I doing wrong?

2条回答
我想做一个坏孩纸
2楼-- · 2019-09-17 05:39

try like so:

$sql = $wpdb->prepare(
     "INSERT INTO `wp_branches` (`user_id`,`branchName`) values (%d,%s)", 
      $user_id, $bname);

$wpdb->query($sql);

this will protect you against "injections" too.

查看更多
Anthone
3楼-- · 2019-09-17 05:58

modified so:

$wpdb->insert('wp_branches', array('user_id' => $user_id, 'branchName' => $bname));
查看更多
登录 后发表回答