你说你的表格在主文件夹里?我想你需要为action=""
. 您可以尝试:
action="<?php echo site_url() . \'/setLiquorType.php\'; ?>"
将表单代码放入函数中是最佳做法。php,然后离开表单操作"E 空的。然后,您可以在提交表单时触发表单函数。
您的表单HTML:
<form method = "post" action = ""> //edited form action
<h3>Add a New Liquor Type</h3> //wrapped title with <h3> tags and removed <br> because h3 element will have padding that will separate it from form (if not provided in your style.css it will be assigned by the browser)
<p> //wraping field with paragraph to generate automatic space between them without styling it
<label for="name">Name:</label> //removed <p> element and used <label> instead
<input type="text" name="name"/>
</p>
<p>
<label for="description">Description</label>
<input type="text" name="description"/>
</p>
<hr> //replaced unnecessary --- lines
<input type="submit" value="Submit" name="liquor_submit"/> // added name attribute that will be used to check if the form is submitted
</form>
</br>
现在在您的功能中。php您可以添加以下内容:
//setting up the form
function themename_add_new_liquor() {
$name = $_POST[\'name\'];
$description = $_POST[\'description\']; //You forgot to collect data from "description" field
global $wpdb; //removed $name and $description there is no need to assign them to a global variable
$table_name = $wpdb->prefix . "liquor_type"; //try not using Uppercase letters or blank spaces when naming db tables
$wpdb->insert($table_name, array(
\'lq_name\' => $name, //replaced non-existing variables $lq_name, and $lq_descrip, with the ones we set to collect the data - $name and $description
\'description\' => $description
),
array(
\'%s\',
\'%s\'
) //replaced %d with %s - I guess that your description field will hold strings not decimals
);
}
//And now to connect the two:
if( isset($_POST[\'liquor_submit\']) ) themename_add_new_liquor();
我希望这能有所帮助。此外,如果您没有首先创建数据库表,则所有这些都不起作用(
$wpdb->insert
函数用于在现有表中插入数据)。