[英]Inserting data into wordpress table using a html form
I am trying for hours to resolve what I thought was a simple issue but am still no further... 我试图花费数小时来解决我认为是一个简单的问题,但现在还没有进一步……
Below is my code to input 3 fields into a wordpress custom table called 'std'. 下面是我的代码,将3个字段输入到称为“ std”的wordpress自定义表中。 Not sure if it is a connection issue or my code is just wrong! 不知道这是连接问题还是我的代码错误! when I click submit, the I am just given the "databse insertion failed" message but am not the wiser as to why. 当我单击“提交”时,系统只会显示“数据库插入失败”消息,但对于原因却不那么明智。
<?php
/*
Template Name:ProRata
*/
//get_header();
if($_POST['Submit']){
global $wpdb;
require_once('wp-config.php');
$name=$POST['aname'];
$roll=$POST['aroll'];
$dept=$POST['adept'];
$tablename = $wpdb->prefix.'std';
if($wpdb->insert(
$tablename,
array(
'name'=> $name,
'roll'=> $roll,
'dept'=> $dept
)
) ==false) wp_die('databse insertion failed');
else echo 'Database insertion completed</p>';
?>
<?php
}
else // if form has not yet been submitted
{
?>
<br>
<form action="" method="post">
<label>Leave: Type of leave </label>
<label> aname </label>
<input type="text" name="aname"><br><br>
<label> Leave: Start Date </label>
<input type="text" name="aroll"><br><br>
<label> Leave: End Date </label>
<input type="text" name="adept"><br><br>
<input type="submit" name="Submit"><br>
</form>
<?php
}
?>
I am not sure if this will totally resolve your issue, but you are missing the underscores ( _
) when you are storing the $_POST
values. 我不确定这是否可以完全解决您的问题,但是在存储$_POST
值时,您缺少下划线( _
)。 You need to change those lines to the following: 您需要将这些行更改为以下内容:
$name=$_POST['aname'];
$roll=$_POST['aroll'];
$dept=$_POST['adept'];
finally got the answer.... 终于得到答案了。
Change this
$name=$POST['aname'];
$roll=$POST['aroll'];
$dept=$POST['adept'];
$tablename = $wpdb->prefix.'std';
With this
$name=$_POST['aname'];
$roll=$_POST['aroll'];
$dept=$_POST['adept'];
$tablename = 'std';
Error on insertion is because it's trying to insert into table "wp_std", assuming that the prefix on your wordpress is wp_, or any prefix. 插入错误是因为假设您的wordpress上的前缀是wp_或任何前缀,它试图插入表“ wp_std”。 Also I changed $POST with $_POST, because even if insertion where true, it would insert empty values because $POST doesnt exists unleast you declare it, correct global variable is $_POST 我也用$ _POST更改了$ POST,因为即使在true处插入,它也会插入空值,因为$ POST不存在,除非您声明它,正确的全局变量是$ _POST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.