PHP: Search Record from MySQL in wordpress CMS Using Php Code Plugin

1. MySQL database

asfeef

2. Click on “PHP Code” & Add New PHP Code

dsfdsf





style="display:block"
data-ad-client="ca-pub-7617983520168557"
data-ad-slot="8121025620"
data-ad-format="auto">



3.  Complete code is here

<form action="/wordpress/search-record/" method="post">
<?php
$hid="style='margin: auto; visibility:hidden'";
$codeerr="";
$code="";
$flag=0;
if(isset($_POST["submit"]))
{
if(empty($_POST["t1"]))
{
$codeerr="Data Missing";
$flag=0;
}
else if(is_numeric(trim($_POST["t1"]))==false)
{
$codeerr="Accept Numeric value only";
$flag=0;
}
else
{
$code=$_POST["t1"];
$flag=1;
}
}

if($flag!=0)
{
$server="localhost";
    $username="root";
	$password="";
	$dbname="wordpress";
	$cnt=0;
	try
	{
		$db=new PDO("mysql: host=$server; dbname=$dbname",$username, $password);
		$result=$db->query('select *from students where roll='.$code);

$result->setFetchMode(PDO::FETCH_ASSOC);
$row=$result->fetch();
$cnt=count($row);
if($cnt>1)
{
$hid="style='margin: auto; visibility:visible'";
$rl=$row["roll"]; 
$text="<input type='text' name='ro' value='$rl' readonly='readonly' />";
$r2=$row["name"]; 
$text1="<input type='text' name='na' value='$r2' />";
$r3=$row["course"]; 
$text2="<input type='text' name='co' value='$r3' />";
$btn=""; 

}
else
{
echo"<h2 style=' text-align:center'>Record not Found</h2>";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}

?>
<div style="text-align:center" >
	<input type="text" name="t1"/>
	<input type="submit" name="submit" value="Search"/>
	<br>
	<?php echo $codeerr; ?>
	
	<table <?php echo $hid; ?> >
	<tr>
		<td>Roll No.</td>
		<td><?php echo $text; ?></td>
	</tr>
	<tr>
		<td>Name</td>
		<td><?php echo $text1; ?></td>
	</tr>
	<tr>
		<td>Course</td>
		<td><?php echo $text2; ?></td>
	</tr>
	<tr>
		<td colspan="2" align="right"><?php echo $btn; ?></td>
	</tr>
</table>
</div>
</form>


4.Add New Page use [php snippet=3] shortcut from PHP Code plugin

snippet_codsf5. Run the application

zdfas

6. Enter Student roll no which you want to search & click search button

dsgrfd

 

 

Posted in PHP