Php 我想在数据库中发布的可湿性粉剂插件
我正在使用wordpress中的插件在表格中添加数据,但当我进入submitt按钮时,它不会显示任何内容,也不会显示我出错的数据加载表格??请任何人帮帮我。 我的插入数据代码在这里Php 我想在数据库中发布的可湿性粉剂插件,php,wordpress,Php,Wordpress,我正在使用wordpress中的插件在表格中添加数据,但当我进入submitt按钮时,它不会显示任何内容,也不会显示我出错的数据加载表格??请任何人帮帮我。 我的插入数据代码在这里 <form id="form1" name="form1" method="post" action=""> Name <input type="text" name="name" /> </br> Website <input type="text
<form id="form1" name="form1" method="post" action="">
Name
<input type="text" name="name" /> </br>
Website
<input type="text" name="website" /> </br>
Description
<input type="text" name="description" /> </br>
<input type="submit" value="Submit Form" />
</form>
<?php
global $wpdb;
$wpdb->query($structure);
$wpdb->query="insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')";
if (is_object($wpdb) && is_a($wpdb, 'wpdb')) {
if (!$wpdb->insert('.PRO_TABLE_PREFIX.tutorial',
array(
'name'=>$_POST[name]
,'website'=>$_POST[website]
,'description'=>$_POST[description]
))) exit;
}
else {echo 'Form Submitted';}
?>
名称
网站
描述
空白页面通常意味着内部50倍的错误,通常由PHP或您的web托管软件(可能是Apache)引起
$wpdb->query=
无效。守则应改为:
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
因为$wpdb->query是一个函数,而不是一个变量
更新:(更深入)
首先,让我先链接到。出于您的目的,您需要执行以下操作:
$table = $wpdb->prefix."my_table";
注意:在输入表名时,不要包含“wp_2;”前缀。“wp_u2;”前缀可以通过多种方式更改,但它将始终存储在$wpdb->prefix
中,因此请始终使用此前缀,而不是键入默认前缀
global $wpdb;
$wpdb->insert($table,array(
"name" => mysql_real_escape_string($_POST['name']),
"website" => mysql_real_escape_string($_POST['website']),
"description" => mysql_real_escape_string($_POST['description'])
));
这将把记录输入数据库。mysql\u real\u escape\u字符串对于保护自己免受攻击非常重要。这就是全部
更新2:(对下一条评论的回应)
然后您需要进行PHP检查以查看表单是否已提交。您可以简单地添加if(isset($\u POST)){}
,但我个人不喜欢这样做,因为如果另一个表单通过POST提交到此页面,数据库仍然会更新
<?php if(!isset($_POST[PLUGIN_PREFIX.'submit'])){
global $wpdb;
$table = $wpdb->prefix.PLUGIN_PREFIX."my_table";
// $wpdb->insert will return true or false based on if the query was successful.
$success = $wpdb->insert($table,array(
"name" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'name']),
"website" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'website']),
"description" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'description'])
));
if($success){
$display = '<div class="'.PLUGIN_PREFIX.'submit_success">
Your data has been saved.
</div>';
}else{
$display = '<div class="'.PLUGIN_PREFIX;.'submit_fail">
Your data failed to save. Please try again.
</div>';
}
}
$display .= '<form id="form1" name="form1" method="post" action="">
<label for="name">Name</label>
<input id="name" type="text" name="'.PLUGIN_PREFIX.'name" /> </br>
<label for="website">Website</label>
<input id="website" type="text" name="'.PLUGIN_PREFIX.'website" /> </br>
<label for="description">Description</label>
<input id="description" type="text" name="'.PLUGIN_PREFIX.'description" /> </br>
<input type="hidden" name="'.PLUGIN_PREFIX.'submit" />
<input type="submit" value="Submit Form" />
</form>';
return $display;
添加$wpdb->show_errors()代码>紧跟在全局$wpdb之后代码>你就会知道你的问题是什么。多极:我会更新我的答案,让它更深入一点。给我几分钟。这段代码在我刷新网页时自动添加数据如何摆脱?
<?php if(!isset($_POST[PLUGIN_PREFIX.'submit'])){
global $wpdb;
$table = $wpdb->prefix.PLUGIN_PREFIX."my_table";
// $wpdb->insert will return true or false based on if the query was successful.
$success = $wpdb->insert($table,array(
"name" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'name']),
"website" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'website']),
"description" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'description'])
));
if($success){
$display = '<div class="'.PLUGIN_PREFIX.'submit_success">
Your data has been saved.
</div>';
}else{
$display = '<div class="'.PLUGIN_PREFIX;.'submit_fail">
Your data failed to save. Please try again.
</div>';
}
}
$display .= '<form id="form1" name="form1" method="post" action="">
<label for="name">Name</label>
<input id="name" type="text" name="'.PLUGIN_PREFIX.'name" /> </br>
<label for="website">Website</label>
<input id="website" type="text" name="'.PLUGIN_PREFIX.'website" /> </br>
<label for="description">Description</label>
<input id="description" type="text" name="'.PLUGIN_PREFIX.'description" /> </br>
<input type="hidden" name="'.PLUGIN_PREFIX.'submit" />
<input type="submit" value="Submit Form" />
</form>';
return $display;