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 Answer
Reset to default 3First 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
));
$table_name
. What is the structure of your table? What is the primary key? – Jacob Peattie Commented Jun 26, 2019 at 10:41admin-ajax.php
. This is an important detail. – MikeNGarrett Commented Jun 26, 2019 at 11:54