First wpdb insert to mySql new table isn't working

亡梦爱人 提交于 2019-12-25 06:35:47

问题


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?


回答1:


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.




回答2:


modified so:

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


来源:https://stackoverflow.com/questions/6369468/first-wpdb-insert-to-mysql-new-table-isnt-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!