我有一个word word press的网站我在WordPress的首页上添加了以下HTML自定义字段,这里我从用户那里获取输入数据,我必须将输入数据插入我的WordPress数据库而不使用任何插件。问题是我无法在数据库表中插入数据。所有代码(HTML + php)都在front-page.php文件中。 这是我的HTML代码
<form method="post" action="">
<div style="text-align:left; border: 1px solid #bbb; padding:10px;">
<div>
<label for="name">name</label>
<input type="text" name="name" id="name" />
</div>
<div>
<label for="email">email</label>
<input type="text" name="email" id="email" />
</div>
<div>
<label for="message">message</label>
<textarea name="message" id="message" style="width:250px; height:100px"></textarea>
</div>
<input type="submit" name="signup" value="signup" />
</div>
</form>
这是我的WordPress php代码
<?php wp_reset_query();
$wpdb->insert(
$tbl_custom,
array(
"name" => $name,
"email" => $email,
"mesage" => $massege,
)
);
$Offerid=$wpdb->insert_id;
//var_dump( $wpdb->last_query );
?>
下面是表格结构
custom
CREATE TABLE `custom` (
`name` varchar(20) NOT NULL,
`email` varchar(20) NOT NULL,
`message` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
下面是截图
答案 0 :(得分:0)
我得到的答案是示例将自定义字段数据插入WordPress数据库。
<?php
If($_POST['Submit']) {
// run validation if you're not doing it in js
global $wpdb;
$coursename=$_POST['coursename'];
$coursename1=$_POST['coursevalue'];
$coursecategory=$_POST['coursecategory'];
if($wpdb->insert(
'custom',
array(
'name' => $coursename,
'email' => $coursename1,
'message' =>$coursecategory
)
) == false) wp_die('Database Insertion failed'); else echo 'Database insertion successful<p />';
?>
<a href="" onClick="return false;" id="addform">Add Another Course.</a>
<?php
}
else // else we didn't submit the form, so display the form
{
?><form action="" method="post" id="addcourse">
<label id="coursename">Course Name:<input type="text" name="coursename" size="30" /></label>
<label id="coursevalue">Points Value:<input type="text" name="coursevalue" size="10" /></label>
<label id="coursecategory">Course Category:<select name="coursecategory" size="1">
<option selected>Product Knowledge</option>
<option>Demonstrate Effectively</option>
<option>Perfect Your Pitch</option>
<option>Business Integration and Technical Training</option>
</select></label>
<p> </p>
<p> </p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
<a href="" onClick="return false;" id="addform">Add Another Course.</a>
<?php
} // end else no post['submit']
{
?>