最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - $wpdb insert is not work

programmeradmin1浏览0评论

I try different way for insert data in custom table but is not work and there are not errors.

Sometimes is save, but most of the time is not.

I check around the web, but no solution is working for me.

my last method is:

$sql = $wpdb->prepare("INSERT INTO `$table_name` 
(`name`, `email`, `phone`, `address`, `message`, `rq`, `url`) 
values 
(%s, %s, %s, %s, %s, %d, %s)",
$name, $email, $phone, $address, $msg, 1, $url);

$wpdb->query($sql);

I print $sql and paste on phpmyadmin SQL and save it, so the query doesn't have any problem, but still not save.

Why this happen?

I already try with array and $wpdb->insert(), same problem.

EDIT: it is solved. it was an integer passed like string, thanks to everybody

I try different way for insert data in custom table but is not work and there are not errors.

Sometimes is save, but most of the time is not.

I check around the web, but no solution is working for me.

my last method is:

$sql = $wpdb->prepare("INSERT INTO `$table_name` 
(`name`, `email`, `phone`, `address`, `message`, `rq`, `url`) 
values 
(%s, %s, %s, %s, %s, %d, %s)",
$name, $email, $phone, $address, $msg, 1, $url);

$wpdb->query($sql);

I print $sql and paste on phpmyadmin SQL and save it, so the query doesn't have any problem, but still not save.

Why this happen?

I already try with array and $wpdb->insert(), same problem.

EDIT: it is solved. it was an integer passed like string, thanks to everybody

Share Improve this question edited Jun 26, 2019 at 12:06 Gabriele Carbonai asked Jun 26, 2019 at 10:39 Gabriele CarbonaiGabriele Carbonai 1115 bronze badges 4
  • 1 Where and how are you running this code? Where are your variables defined? Particularly $table_name. What is the structure of your table? What is the primary key? – Jacob Peattie Commented Jun 26, 2019 at 10:41
  • everything is defined and if I print $sql, copy and paste in phpmyadmin, the query is work. – Gabriele Carbonai Commented Jun 26, 2019 at 11:00
  • And I am running this code by my own plugin with ajax. It wired that, same inputs sometimes is saving sometimes is not – Gabriele Carbonai Commented Jun 26, 2019 at 11:06
  • Please add to your question that you're running this after a call to admin-ajax.php. This is an important detail. – MikeNGarrett Commented Jun 26, 2019 at 11:54
Add a comment  | 

1 Answer 1

Reset to default 3

First insert the global.

global $wpdb;

Then You have to check your quotes properly.

$sql = $wpdb->prepare(
    "INSERT INTO `$table_name`      
       (`name`,`email`,`phone`,`message`,`rq`,`url`) 
 values ($name, $email, $phone, $address, $msg, 1, $url)");
$wpdb->query($sql);

OR

$wpdb->insert('$table_name', array(
    'name' => $name,
    'email' => $email,
    'phone' => $phone, // ... and so on
));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论