php - 值未插入 WordPress 表中

标签 php mysql wordpress

我正在尝试在模板文件中插入我开发的自定义表单值。这样我就在 wordprress 数据库中创建了一个 customer_info 表。这是正确的做法吗?

这是我迄今为止尝试过的代码:

<?php
if(isset($_POST['submit']))
{
 global $wpdb;
echo $q=$_GET['q']; 
$a=$_POST['name'];
$b=$_POST['lname'];
$c=$_POST['email'];
$d=$_POST['mobile'];
$e=$_POST['phone'];
$f=$_POST['accom'];
$g=$_POST['adress'];
$h=$_POST['remark'];
$wpdb->insert( 'customer_info', array( 'post_id' => $q, 'name' => $a,'lname' => $b, 'email' => $c,'mobile' => $d, 'phone' => $e,'adress' => $g, 'remark' => $h  ), array( '%d', '%d','%s', '%s' ,'%s', '%s' ,'%s', '%s'  ) );
}

?>

为什么它不起作用?通过同一过程,我在本地主机上插入了两个表单值。

正如在核心 php 中一样,我知道如何调试等,所以请告诉我如何在 WordPress 中做到这一点,因为 WordPress 有自己的用于数据库操作的 wpdb 类。

这是我的 html 表单

<form method="post" action="">
<table width="50%" align="left">
<h3>Customer Info</h3>
<tr>
<td valign="top">Name</td><td valign="top"><input type="text" name="name" /></td>
</tr>
<tr>
<td valign="top">Last Name</td><td valign="top"><input type="text" name="lname" /></td>
</tr>
<tr>
<td valign="top">Email</td><td valign="top"><input type="text" name="email" /></td>
</tr>
<tr>
<td valign="top">mobile</td><td valign="top"><input type="text" name="mobile" /></td>
</tr>
<tr>
<td valign="top">Phone</td><td valign="top"><input type="text" name="phone" /></td>
</tr>


<tr>
<td valign="top">Address</td><td valign="top"><input type="text" name="adress" /></td>
</tr>

<tr>
<td valign="top">Remark</td><td valign="top"><input type="textarea" name="remark" /></textarea></td>
</tr>
<tr>
<td valign="top">Click for continue</td><td valign="top"><input type="submit" name="submit" value="Continue" /></td>
</tr>
</table>
</form>

如果我错过了什么,请随时询问。

非常感谢。

最佳答案

你可以尝试这样的错误

1) Enable database error display via $wpdb->show_errors()
2) Check what query is being formed and run via $wpdb->last_query

name您的情况下的字段是一个字符串,并且您已将其定义为插入数组中的整数,请使用 '%s'为此,而不是 %d 。 也使用不同的name输入的属性,因为 name是保留字,可能是这个原因。

关于php - 值未插入 WordPress 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19533213/

相关文章:

php - 动态匹配多个类别的项目

php - 帖子的 WordPress 术语列表

php - 在 WooCommerce 电子邮件模板上使用的短代码中传递订单 ID

PHP:WAITING时间,因此函数直到 x 分钟后才能执行

php - Laravel 5.3 - 用户收集的单一通知(关注者)

mysql - 如何使用触发器拆分数据并将其传输到另一个表

mysql - 仅使用实体关系图查询 MYSQL 格式 [脑力挑战]

css - 为什么这个li :last-child not work?

php - 如何使用 Yii 查询构建器创建复杂查询

php - 使用 php 和 mysql 上传多个图像并插入到表中的单独字段中