how to display query data from php coding in wordpress
-
Hello dear,
I want to use form at my wordpress page for that i have created a form there using wordpress admin pannel. i have written
<form id="search" action="../contact_search.php" method="post" name="search">
for contact_search.php i have written some code in php and uploaded it into my cpanel in the sub directory under theme directory.
Now the problem is this when I am opening my website and posting data from form using submit button it is not displaying contact_search.php page it is showing 404 error.
You can see my website http://www.lamartinierelucknow.org/alumnus-records
Kindly tell me how i can i use my php page i am very new on wordpress
-
It’s because you are posting to
http://www.lamartinierelucknow.org/contact_search.phpand that means the file should be at your WP’s root directory.A better way to achieve what you want is to create a custom page template inside your theme. Then assign that template to a WordPress Page. The template file will then have both your PHP code and HTML for the form.
Here are some examples that could help you get started:
http://www.webdesign.org/how-to-create-contact-page-template-in-your-wordpress-theme.22210.html
http://www.paulund.co.uk/creating-wordpress-custom-page-templatestry to run the file with full path to see if will work.
action="http://server/path/filename.php"you mean i should write action=”http://lamartinierelucknow.org/../contact_search.php”
i am not getting what to write in path… my file is in
wp-content/themes/lamart-theme/contact_search.phphi jomsky,
Thank you very much.
Dear I went to the link which you have given in your post and created a template.
now I am able to get my form by my php page.
now i want to know how to call my php page in which i have written query on post data. could you please tell me that ho can i search data which i am filling in my form.now i want to know how to call my php page in which i have written query on post data
Are you referring to the content of
/contact_search.php? If so you can place it on the same template file that you created like on the examples.This is a template which i have made using your tutorial….
<?php
/*
Template Name: alumni
*/?>
<?php get_header(); ?>
<div id=”primary”>
<div id=”content” role=”main”>
</div><!– #content –>
</div><!– #primary –><h3>Fill in <b>atleast one</b> of the fields below.</h3>
<form id=”search” action=”<?php bloginfo(‘template_url’); ?>/contact_search.php” method=”post” name=”search”>
<table style=”height: 448px;” width=”404″>
<tbody>
<tr>
<td>PERSONAL NO.</td>
<td><input name=”pn” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>FIRST NAME</span></td>
<td><input name=”fn” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>MIDDLE NAME</span></td>
<td><input name=”mn” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>SURNAME:</span></td>
<td><b></b><input name=”sn” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>FATHER’S NAME:</span></td>
<td><input name=”fan” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>FATHER’S OCCUPATION:</span></td>
<td><input name=”fo” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>ADDRESS:</span></td>
<td><input name=”ad” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>DATE OF BIRTH
</span></td>
<td><input id=”datepicker” name=”textfield” type=”text” />
<script>// <![CDATA[
jQuery(document).ready(function() { jQuery(‘#datepicker’).datepicker({ dateFormat : ‘dd-mm-yy’ }); });
// ]]></script></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>YEAR OF ADMISSION:</span></td>
<td><input id=”datepicker2″ name=”textfield2″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>CATEGORY:</span></td>
<td><input name=”cate” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>YEAR OF LEAVING</span></td>
<td><input name=”dl” size=”18″ type=”text” /></td>
</tr>
<tr>
<td><span style=”font-family: arial;”>REASONS FOR LEAVING:</span></td>
<td><input maxlength=”255″ name=”rl” size=”18″ type=”text” /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td style=”text-align: center;”><input name=”submit” type=”submit” value=”Search Now” /></td>
<td style=”text-align: center;”>
<p style=”text-align: center;”><input name=”Reset” type=”reset” value=”Reset” /></p>
</td>
</tr>
</tbody>
</table>
</form>and now in contact_search.php i am writing…..
<?php
$DBServer = ‘xxxxx’;
$DBUser = ‘xxxxx’;
$DBPass = ‘xxxxx’;
$DBName = ‘xxxxx’;$conn = new mysqli($DBServer,$DBUser,$DBPass,$DBName);
if ($conn->connect_error) {
trigger_error(‘Database connection failed: ‘ . $conn->connect_error, E_USER_ERROR);
}$sql=”select * from student where (Pers_ No=’$_POST[pn]’) or (F_Name=’$_POST[fn]’) or (M_Name=’$_POST[mn]’) or (SIR_Name=’$_POST[sn]’) or (Father_Name=’$_POST[fan]’) or (Address=’$_POST[ad]’) or (dob=’$_POST[textfield]’) or (D_ADMI=’$_POST[textfield2]’) or (D_Leave=’$_POST[dl]’)”;
$result = $conn->query($sql);
if($result === false) {
trigger_error(‘Wrong SQL: ‘ . $sql . ‘ Error: ‘ . $conn->error, E_USER_ERROR);
} else {
$arr = $result->fetch_all(MYSQLI_ASSOC);
}?>
<table width=”600″ border=”1″ cellpadding=”1″ cellspacing=”1″>
<tr>
<th>Personal No.</th>
<th>Date of Admission</th>
<th>Sur Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Date Of Birth</th>
<th>Father’s Name</th>
<th>Occupation</th>
<th>Address</th>
<th>Date Of Leaving.</th>
</tr>
<tr>
<?phpforeach($arr as $row) {
echo”<tr>”;
echo “<td>”.$row[‘Pers_ No’].”</td>”;
echo “<td>”.$row[‘D_ADMI’].”</td>”;
echo “<td>”.$row[‘SIR_Name’].”</td>”;
echo “<td>”.$row[‘F_Name’].”</td>”;
echo “<td>”.$row[‘M_Name’].”</td>”;
echo “<td>”.$row[‘DOB’].”</td>”;
echo “<td>”.$row[‘Father_Name’].”</td>”;
echo “<td>”.$row[‘OCCU’].”</td>”;
echo “<td>”.$row[‘Address’].”</td>”;
echo “<td>”.$row[‘D_Leave’].”</td>”;
}
?>but it is showing nothing
Yes but you have to put the code of contact_search.php ON the same file as the HTML code as well.
You will just then need a condition to determine that your form has been posted. Something like:
if ( isset( $_POST['submit'] ) ) { //the form has been submitted, process the data //place your contact_search.php code here } else { //the form has not yet been submitted //show your form here (HTML) }Then change your form action to
""so the form will post to itself. No need to point it anymore to contact_search.php since it’s inside your file.thank you very much jomsky….i am just near to my solution.
now the form is coming correct and posting it correctly but not retriving data from database.
I want to inform you that i have created a database on my cpanel with name “lamar5gn_AlumniRecords” and in this database i have created a table name “student”
i want to retrive data whose value is equal to any of field which is given in my form.
could you please tell me how to retrive data in wordpress.i am writing this code for after posting page but it is showing nothing
<?php
if ( isset( $_POST[‘submit’] ) )
{
$DBServer = ‘xxxxx’;
$DBUser = ‘xxxxx’;
$DBPass = ‘xxxxx’;
$DBName = ‘xxxxx’;$external_users_db = new wpdb($DBUser,$DBPass,$DBName,$DBServer);
$arr=$external_users_db->;get_results( “select * from student where (Pers_ No=’$_POST[pn]’) or (F_Name=’$_POST[fn]’) or (M_Name=’$_POST[mn]’) or (SIR_Name=’$_POST[sn]’) or (Father_Name=’$_POST[fan]’) or (Address=’$_POST[ad]’) or (dob=’$_POST[textfield]’) or (D_ADMI=’$_POST[textfield2]’) or (D_Leave=’$_POST[dl]'”);echo “<table>”;
echo “<tr>
<th>Personal No.</th>
<th>Date of Admission</th>
<th>Sur Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Date Of Birth</th>
<th>Father’s Name</th>
<th>Occupation</th>
<th>Address</th>
<th>Date Of Leaving</th>
</tr>”;
foreach($arr as $row)
{
echo “<tr><td>”.$row[‘Pers_ No’].”</td><td>”.$row[‘D_ADMI’].”</td><td>”.$row[‘SIR_Name’].”</td><td>”.$row[‘F_Name’].”</td><td>”.$row[‘M_Name’].”</td><td>”.$row[‘DOB’].”</td><td>”.$row[‘Father_Name’].”</td><td>”.$row[‘OCCU’].”</td><td>”.$row[‘Address’].”</td><td>”.$row[‘D_Leave’].”</td></tr>”;
}
}
?>After your $wpdb->get_results() call try adding:
$wpdb->show_errors(); $wpdb->print_error();This will help you troubleshoot your SQL problem.
https://codex.wordpress.org/Class_Reference/wpdb#Show_and_Hide_SQL_Errorsthanks dear,
getting correct result now.
one thing i want to ask that im using
$arr=$wpdbtest_maindb->get_results($wpdbtest_maindb->prepare(“SELECT * FROM student WHERE Pers_No = %d OR D_ADMI=%s OR Surname = %s OR F_Name=%s OR M_Name=%s OR Father_Name=%s OR Address=%s”,$pno,’$DADMI’,$SrName,$FName,$MName,$FtName,$Add));
can you please tell me that how to retrive date from database using above given method.
for example i am using %s for string and %d for numeric value…what for date?
The topic ‘how to display query data from php coding in wordpress’ is closed to new replies.