Get sql data using search box in wordpress - php

What I want to do is using search box to find data in WordPress database and display it in a page,using contact form 7. I upload data in a wp database. I use some PHP code to display data in a page and it works, but I don't know how to do it using a search box.
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM it_testtable");
echo "ID"." "."Name"."<br><br>";
foreach($result as $row)
{
echo $row->id." ".$row->name."<br>";
}
?>
I want when I enter the an id I get names. Please help me. Thank you.

Step 1 Add the following to your Header.php file
<div class="header-search"><?php get_search_form();?></div>
Step 2 Add the following to your Style.css file
.header-search{display:none}
#media screen and (min-width:600px){
.header-search{
display:block;
float:right;
margin-top:0px;
margin-right:3px;
}
}
Show search query in search box
If you have just performed a search, you can show the last query in the search box:
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Show search query in results page
You can display the search string on search result pages
<p>You searched for "<?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p>
for reference: About search box query
You can go either this or other option,because there is only name
and ID in your row
$mylink = $wpdb->get_row( "SELECT * FROM {$wpdb->links} WHERE link_id
= 10" );
Else if you want to show it generically,
$fivesdrafts = $wpdb->get_results(
"SELECT link_ID,name FROM {$wpdb->links} WHERE link_ID = 5"
);
foreach ( $fivesdrafts as $fivesdraft )
{
echo $fivesdraft->name;
}

Related

PDO MySQL Update not working correctly on edit page

A 'quiz' currently contains a quiz id, name, description and topic (from a topic table).
I am wanting to setup a simple 'Edit Quiz' page.
The problem is - if a quiz is called 'Quiz1' and I change the quiz name on the edit page to 'Quiz2', once the save button is clicked it will revert back to 'Quiz1' and not stored.
I have setup an echo as shown in the code to check that they are actually getting stored, after the save button is clicked it would show 'Quiz1' but this value is not stored in my database. The SQL Has been tested on PhpMyAdmin and seems to work too.
PHP Code:
<? if (!empty($_POST)) {
$id = $_POST['qzid'];
$qzname = $_POST['qzname'];
$qzdesc = $_POST['qzdesc'];
$ctname = $_POST['ctname'];
$checkQuiz = $db->prepare("SELECT qz_name FROM quizzes WHERE qz_name = :qz_name");
$checkQuiz->execute(array(':qz_name' => $qzname));
$qzChanged = "Quiz details updated successfully";
$sql = "UPDATE quizzes SET `qz_name` = :qzname, `qz_desc` = :qzdesc WHERE `quizzes`.`id` = :qzid";
$q = $db->prepare($sql);
$q->execute(array(':qzname' => $qzname, ':qzdesc' => $qzdesc, ':qzid' => $id));
echo $qzname, $qzdesc; //THIS RETURNS THE CHANGED VALUES
}?>
HTML Code:
<form action="edit_quiz.php?id=<?php echo $row['id'] ?>" method="POST">
<input type="hidden" name="qzid" id="qzid" value=""/>
<!-- selection box -->
<p>Topic Name:
<select class="form-control" name="ctname" id="ctname">
<?php
while ($tresult = $stmt->fetch()) {
echo "<option>" . $tresult["ct_name"] . "</option>";
}
?>
</select>
</p>
<p>Quiz Name: <input type="text" name="qzname" value="<?php echo $row['qz_name']; ?>"/></p>
<p>Quiz Description: <textarea name="qzdesc" value=""/><?php echo $row['qz_desc']; ?> </textarea></p>
<input type="submit" class="btn btn-success" value="Save"/> <a class="btn" href="quizzes.php">Back</a>
Please note this code has been simplified.
Any help I would appreciate very much thanks!
You are not posting the id it looks like.
Is your database named localhost, otherwise remove that part.

Loading comments from Database

I am developing a simple forum for my php project. I have a viewpost.php file which displays all the posts and has a read more button.like this.
<?php
foreach($rows as $output){
echo '<div id="dd">';
$uid = $output['uid'];
$result2 = $conn->prepare("SELECT username FROM users WHERE uid = '$uid'");
$result2->execute();
$rows2 = $result2->fetchAll();
foreach($rows2 as $username_row){
echo 'POST BY '.$username_row['username'].':';}
echo '<h3>Title:'.$output['title'].'</h4>';
?>
<form action="comments.php" method="post">
<input type="submit" value="Read more" name="">
</form>
<?php
echo '</div>';
}
?>
now I want to load post and comments in comments.php. but I want post_id to retrieve post,title and comments from the database. I wonder how to pass the post_id from viewpost.php to comments.php when the user clicks on the readmore button.any help will be really appreciable am a newbee.
You can add a input to your form
<input type="hidden" value="<?php echo $output['id'] ?>" name="id" />
and in your comments.php file use $_POST['id']
or use URL like :
<a href="comments.php?id='<?php echo $output['id'] ; ?>'">
and in your comments.php file use $_GET['id']

Search results are not returning from sql query using search button

I'm trying to get a search functionality working. When the user clicks in a box and searches something the page should be able to display the results with the word included, otherwise there is a simple message that no result could be found. At the moment nothing is displaying (no errors either) and I do not know why.
Here is what I have so far. Any help would be appreciated.
1) On my index.php page there is the search box and search button.
<li>
<div id = "search">
<form id = "search-form" action="search_results.php" method="post">
<input type="text" name= "find" placeholder= "Search..." />
<input type="button" name = "search" value="search" />
</form>
</div>
</li>
2) When the user types what they want, they are taken to search_results.php where the searching happens. A list of product names should be returned if the search was successful.
<div class = "main-content">
<h1> Search Results </h1>
<?php
include 'dbconnect.php';
$output ='';
if (isset($_get['find'])){
$searchq = $_get['find'];
$query = mysqli_query($con, "SELECT * FROM products WHERE prodname LIKE '%". $searchq . "%'");
$count = mysqli_num_rows($query);
if($count == 0){
echo "There was no search results !";
}
else{
while($row = mysqli_fetch_array($query)){
$prodname = $row['prodname'];
$output ='<div> '.$prodname.'</div>';
echo $output;
}
}
} // end of outer if
mysqli_close($con);
?>
</div>
You are doing post and using the $_get in your search_results.php file which is wrong method.
As you are using the post method in your action you should get the values accordingly
i.e.,
if (isset($_POST['find'])){
$searchq = $_POST['find'];
#Your If Condition
}
else
{
#Your Else Part
}

Display search result on another page

can you help me with displaying output from search form ?
My search form is in sidebar and i would like to display result on main content div.
Navigation is done by switch
this is form in sidebar
<form name="searchform" method="get" action="index.php?page=search">
<input type="text" name="searchword" size="15" />
<input type="submit" name="search" value="Click" class="formbutton" />
</form>
this is switch that should include output to the main content:
switch($_GET['page']){
case "search":
include("includes/search.php");
break;
}
and this is included search.php which should display result:
<?php
include('connect.php');
if(isset($_GET['search'])){
$search_value = $_GET['searchword'];
$query = " SELECT * FROM Articles WHERE keywords LIKE '$search_value'";
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$post_id = $row['id'];
$post_title = $row['title'];
echo "<p><a href='index.php?page=post&&post=$post_id'>$post_title</a></p></ br>";
}
}
?>
What about if you specify a different page entirely in the form action instead of index.php? What you did is tell the form to use the index.php and send the GET data to it. If that doesn't help then be more specific on what you want

List option values fail to insert into the database

I've created a php form to insert values into a database.
One of my form options is a dynamic list populated with fields from another table.
I first created the form without the dynamic option, and all data inserted just fine (and still does).
Now I'm attempting to include the code below, and while it displays the option values properly, the value fails to insert. Any advice?
<?php
/*
* LIST ALL CATEGORIES
****************************************/
include('../dbconnection.php');
$query = 'SELECT category_id, category_name FROM ingredient_categories';
$result = mysql_query($query);
echo '<select>';
while ($ingredientCategoryOption = mysql_fetch_array($result)) {
echo '<option value="'.$ingredientCategoryOption[category_id].'">'.$ingredientCategoryOption[category_name].'</option>';
}
echo '</select>';
?>
I had created something similar yesterday. The $polls array is passed to the view in CodeIgniter in the $this->load->view('poll.php', $data['polls']), while you do it in the page itself. However, you can have the general idea.
<FORM id="formPoll" class="question" name="createpoll" action="<?php echo base_url()?>index.php/poll/selectOption/" method="POST">
<select name="poll_list">
<?php
foreach($polls as $poll){
echo "<option name='poll_table'>$poll->name</option>";
}
?>
</select>
<div id="input">
Poll name: <input type="text" name="name"></input>
Title: <input type="text" name="title"></input>
</div>
<div id="options">
Option: <input type="text" name="option"></input>
</div>
<input type="submit"></input>
</FORM>
Some ideas:
Check if $results is not empty before using it
Give your <select> form a name, as I showed above
Check if $ingredientCategoryOption is not null or is something returned.
Check your database connection

Categories