我正在尝试在模板文件中插入我开发的自定义表单值。这样我就在 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/