问题
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