本教程将使用锻炼跟踪器作为示例来展示如何为数据收集构建WordPress表单。 表格收集俯卧撑,锻炼日期和用户登录详细信息。 数据库连接脚本改进了跨多个文件的数据库凭证管理。 PHP文件处理形成数据,为数据库插入准备。 提供了故障排除的提示和资源。
钥匙要点:
>加载了一个包含图形库的基本插件。
创建了一个用占位符短码的空白帖子。
>用WordPress注册了短代码。>此步骤构建了用于记录俯卧撑锻炼的简单形式。 OPEN
(在> 中),并用以下php代码替换其内容:
来输出表单的HTML。 该表单被添加到前面创建的“添加俯卧撑”帖子中(仅包含custom-workout.php
短代码)。 添加代码后,清除WP缓存以查看更改。Plugins
Editor
<?php function pushups() { get_currentuserinfo(); $current_user = wp_get_current_user(); $wpuser = $current_user->user_login; $currentpage = $_SERVER['REQUEST_URI']; echo '[Login]('.$currentpage.') to start adding pushups!'; echo '[Register]('.$currentpage.') if you have not already.'; echo '<form method="get" action="add_pushups.php">'; echo '<label>Number of Pushups: <input type="number" name="pushups_count"></label><br>'; echo '<label>Date of Workout: <input type="date" name="pushups_date"></label><br>'; echo '<input type="hidden" name="pushups_wpuser" value="'.$wpuser.'">'; echo '<input type="hidden" name="current_page" value="'.$currentpage.'">'; echo '<input type="submit" value="Submit">'; echo '</form>'; echo '---'; } add_shortcode('pushups_sc', 'pushups'); ?>
>提交表单(尽管尚未函数)将数据传输演示为下一个处理步骤。 造型可以稍后添加。echo
[pushups_sc]
>
>
,用数据库凭据替换包围的值(可从您的托管提供商获得):
>将此文件上传到>文件夹。
>connect_to_db.php
步骤3:使用PHP
<?php /* Database connection details */ function connect_to_db() { $username = "{username}"; $password = "{password}"; $hostname = "{hostname}"; $dbname = "{database_name}"; mysql_connect($hostname, $username, $password) or die(mysql_error()); mysql_select_db($dbname); } ?>
创建 此文件使用 >检查您的工作和故障排除>
语句来识别问题。 常见问题包括错别字和错误的数据库连接详细信息或标题位置语法。 成功插入数据后,验证phpMyAdmin中的数据。 >
recap:>
>plugins/flot-for-wp/flot
并粘贴此代码:<?php
function pushups() {
get_currentuserinfo();
$current_user = wp_get_current_user();
$wpuser = $current_user->user_login;
$currentpage = $_SERVER['REQUEST_URI'];
echo '[Login]('.$currentpage.') to start adding pushups!';
echo '[Register]('.$currentpage.') if you have not already.';
echo '<form method="get" action="add_pushups.php">';
echo '<label>Number of Pushups: <input type="number" name="pushups_count"></label><br>';
echo '<label>Date of Workout: <input type="date" name="pushups_date"></label><br>';
echo '<input type="hidden" name="pushups_wpuser" value="'.$wpuser.'">';
echo '<input type="hidden" name="current_page" value="'.$currentpage.'">';
echo '<input type="submit" value="Submit">';
echo '</form>';
echo '---';
}
add_shortcode('pushups_sc', 'pushups');
?>
$_GET
>检索表单数据,将日期转换为UNIX时间戳,然后将数据插入数据库。 评论说明语句对于调试数据库连接问题很有用。将此文件上传到echo
>。plugins/flot-for-wp/flot
echo
>
以上是WordPress中响应敏感的实时图:形式处理的详细内容。更多信息请关注PHP中文网其他相关文章!