我无法使用WP插件从Mysql获取数据 [英] I Can't get data from Mysql using a WP plugin

查看:70
本文介绍了我无法使用WP插件从Mysql获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我创建了一个WP插件,它是一个用户可以提交一些数据的表单。

首先看一下代码

Hello ,
I create a WP plugin , it is a form that user can submit some data.
lets first look at the code

<?php
/*
Plugin Name: My Plugin
Description: test plugin
Version: 4.0
Author: Hercal
License: GPL
*/
include('db.php'); //database connection file located in same folder .
?>



我开始使用以下表格:


I began my form with :

<?php function form_creation(){?>
<form action=" <?php get_permalink(); ?> " method="post" id="myform">
<table>

<tr>
<td><input type="text" id="txtname" name="txtname" placeholder="Your Name"/> </td>
</tr>

<tr>
<td>
<select id='select_Country' name="select_Country" >
		<option selected="selected" disabled="disabled"> -- Select Country -- </option>
	    <?php
		include(db.php);
        $query="select Code,Country from _Country order by Country";
        $result=mysqli_query($con,$query);
        while($row=mysqli_fetch_array($result))
        {
            echo '<option value='.$row["Code"].'>'.$row["Country"].'</option>';
        }
        ?>
</select> 

</td>
</tr>
<tr>
<td>  <input type="submit" id="btnsubmit" value="Submit" name='submit'/> </td>
</tr>
</table>
</form>
<?php }?>



及以下是在同一页面中将值插入数据库的php代码


and below is the php code in same page to insert values into DB

<?php
include(db.php);
if($_POST['submit'])
{
	$name=strip_tags($_POST['txtname']);
	$country=$_POST['select_Country'];
$insertquery="insert into _customers(Name,Country)values('$name',(select Country from _country where Code = '$country')";
	$query=mysqli_query($con,$insertquery);
	if($query)
	{
		echo 'Data inserted successfully';
	}
	else
	{
		echo 'there is a problem<br/>';
		print_r( (mysqli_errno($con)));
	}
}
?>





,最后在同一页面下面的行下方为该表单创建一个短代码



and finally below line also in same page to create a short code for this form

<?php add_shortcode('test', 'form_creation');?>





现在,当我使用(测试)短代码并将其放在我的wordpress页面上,表单已加载,显示但有一个问题。



1国家/地区字段未从数据库加载,因此只有一个选项的下拉负载

,选择国家



请记住:

1 - 当我输入名称并按提交时,名称已成功插入数据库,这意味着db.php没有问题,为什么选择国家/地区查询没有返回值。



2 - 当我在root上运行相同的代码(没有WP)时,它工作正常并且国家/地区已成功加载但是当我使用ShortCode从WordPress调用这个插件,问题发生了



我需要你的支持,谢谢。



Now , when i use (test) shortcode and put it on my wordpress page , form loaded ,displayed but there is one problem .

1-Country field didn't load from database , so the drop down load with only one choice
,Select Country

keep in mind :
1-when I Enter a name and press submit , name inserted successfully to DB which means that db.php has no problem , so why the select country query didn't return values.

2-when i run the same code on root(without WP) , it works fine and country loaded successfully but when i call this plugin from WordPress using ShortCode , problem happens

I need your support , thanks .

推荐答案

query = 从国家/地区按国家/地区选择代码,国家;
query="select Code,Country from _Country order by Country";


result = mysqli_query(
result=mysqli_query(


con,


这篇关于我无法使用WP插件从Mysql获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆