我正在尝试构建一个插件,该插件将允许管理员添加有关特定软件包的数据(就像创建User一样)。在前端,我将有一个表单,该表单将允许访问者使用由管理员分配给该包裹的ID来搜索特定包裹。
我使用php构建了系统,并且工作正常。现在,我想将其转换为WordPress插件,并且发现很难将表单数据发送到激活插件时创建的wpdb表(wp_demo)。
这是表格:
function registration_init(){
echo '<form action="demo.php" method="post" />';
echo "<h1>Britchi Tracking</h1>";
echo '<p>';
echo 'Costumers Name (required) <br/>';
echo '<input type="text" name="cname" placeholder="Emmanuel John"/>';
echo '</p>';
echo '<p>';
echo 'Tracking number (required) <br/>';
echo '<input type="text" name="ctracking" placeholder="EEX28PDS"/>';
echo '</p>';
echo '<p>';
echo 'Email (required) <br/>';
echo '<input type="email" name="cemail" placeholder="Email"/>';
echo '</p>';
echo '<p>';
echo 'Recived Port (required) <br/>';
echo '<input type="text" name="crport" placeholder="Brazil Port"/>';
echo '</p>';
echo '<p>';
echo 'Delivered Port (required) <br/>';
echo '<input type="text" name="cdport" placeholder="Lagos Port"/>';
echo '</p>';
echo '<p>';
echo 'Current Location (required) <br/>';
echo '<input type="text" name="clocation" placeholder="Abuja" />';
echo '</p>';
echo '<p>';
echo 'Destination (required) <br/>';
echo '<input type="text" name="cdestination" placeholder="Total filling Station, Abuja"/>';
echo '</p>';
echo '<p><input type="submit" name="cregistered" value="Register"></p>';
echo '</form>';
}
这是表单处理代码:
<?php
define('DB_NAME', 'wordpree2');
define('DB_USER', 'root');
define('DB_PASSWORD', 'chimezie12');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
$db_selected = mysql_selected_db(DB_NAME, $link);
if (!$db_select) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$cname = $_POST['cname'];
$cemail = $_POST['cemail'];
$ctracking = $_POST['ctracking'];
$crport = $_POST['crport'];
$cdport = $_POST['cdport'];
$clocation = $_POST['clocation'];
$cdestination = $_POST['cdestination'];
$sql = "INSERT INTO `wp_demo` (cname, cemail, ctracking, erport, cdport, clocation, cdestination) VALUES ('$cname', '$cemail', '$ctracking', '$crport', '$cdport', '$clocation', '$cdestination')";
if (!mysql_query($sql)) {
die('ERROR: ' . mysql_error());
}
mysql_close();
?>
我知道这不是将数据插入wpdb表的正确方法,但是我不知道该怎么做。
答案 0 :(得分:2)
仅是一个简短示例,说明如何以Wordpress
的方式在数据库中插入数据:
<?php
global $wpdb;
$wpdb->insert('wp_demo', [
'cname' => $_POST['cname'],
'cemail' => $_POST['cemail'],
'ctracking' => $_POST['ctracking']
], ['%s','%s', '%s']
);
有关完整参考,请参见:https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row
此外,不要忘记清理输入内容: https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data