第一个wpdb插入到mySql新表中不起作用 [英] First wpdb insert to mySql new table isn't working

查看:78
本文介绍了第一个wpdb插入到mySql新表中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的mySql表,我需要第一个字段作为索引和键.
我不确定我使用的术语是否正确,但是我只需要该字段在每次插入时自动加1.
因此,我将该字段定义为index,并为其指定了auto_increment属性.
现在,我尝试像这样插入第一行:

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));

此查询中缺少索引/键字段branchId,因为我希望数据库是第一个插入,因此会自动为其赋予值1,然后每增加一个插入就将其递增.
由于某些原因,未插入该行,并且db保留为空.
我在做什么错了?

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?

推荐答案

尝试如下:

$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.

这篇关于第一个wpdb插入到mySql新表中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆