<?php
$con=mysqli_connect("localhost","root","","ok_db")or die(mysqli_connect_error());
$output = 'arslan';
// collect
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query($con,"SELECT * FROM user_data WHERE fname LIKE '%$searchq%'") or die("Could not search.");
$count = mysqli_num_rows($query);
if($count == 0) {
$output = 'No results found.';
} else {
while($row = mysqli_fetch_array($query)) {
$itemname = $row['fname'];
$description = $row['lname'];
$image = $row['id'];
$output .= '<div>'.$itemname.' '.$description.'</div>';
}
}
}
else{
echo "no" ;
}
?>
<html>
<head>
<title>searching</title>
</head>
<body>
<form action="search.php" method="POST">
<input type="text" name="search" placeholder="Search">
<input type="submit" value=">>" />
</form>
</body>
<?php
print $output;
?>
</html>
This code works fine on my local host (XAMPP) but does not echo anything out in PhpStorm, the isset function not working there and always shows the output "no".
Is something wrong with my PhpStorm settings because it runs fine on localhost?
PHP STORM is an IDE for writing your code and has no effect on this.
I would suggest doing
print_r($_POST['search']);
and making sure it is actually filled in, possibly a typo.
Related
I am currently creating a quiz using PHP, HTML and mySQL for simply practice. However, I keep running into a problem where, the question displayed is different to the actual question linked to the answer. So where the user is correct, the answer is wrong.
Here is what I have for the question page:
<?php
session_start();
session_unset();
$link = mysqli_connect("xxxx","xxxx","xxxx", "xxxx");
if(mysqli_connect_error()) {
echo "There was an error connecting to the database!";
}else{
$qNo = rand(1,2);
$query = "SELECT * FROM questions WHERE questionID = '".$qNo."'";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
$answer = $row[2];
$question = $row[1];
if(isset($_POST['submit'])){
$_SESSION['question'] = $row[1];
$_SESSION['answer'] = $answer;
$_SESSION['userAnswer'] = $_POST['userAnswer'];
header("Location: process.php");
}
}
?>
<html>
<head>
<title>Quiz v2</title>
</head>
<body>
<h1>Welcome to Quiz!</h1>
<div>
<p><a><?php print_r($question);?></a></p>
<form method="post">
<label for="answer">Answer: </label><input name="userAnswer">
<button name="submit">Answer</button>
</form>
</div>
</body>
</html>
This is what I have for the process page:
<?php
session_start();
$link = mysqli_connect("xxx","xxxx","xxxx", "xxxx");
if(mysqli_connect_error()) {
echo "There was an error connecting to the database!";
}else{
echo $_SESSION['userAnswer'];
echo $_SESSION['answer'];
echo $_SESSION['question'];
if($_SESSION['userAnswer']==$_SESSION['answer']){
}
}
?>
Over here you can see the question displayed is:
enter image description here
But this is what is actually being asked and compared:
enter image description here
On my website I have a file articles.php and on it I have a search field. When I enter the information it redirects me to my search.php which is correct and in the URL I can see it is outputting my result but it's not showing me my results on the search.php body.
The Localhost URL is outputting after I searched "Can you game on Windows visa"
http://localhost/qaa/search.php?search=Can+you+game+on+widows+vista&submit-search=
There is nothing on this page, just an empty box search.php
Connection
<?php
$server = "localhost";
$username = "root";
$password = "";
$db = "Qaa";
$conn = mysqli_connect($server, $username, $password, $db);
?>
articles.php "Where my search bar is located"
<?php
include 'connect.php';
?>
<h1>Front Page</h1>
<h2>All articles:</h2>
<link rel="stylesheet" type="text/css" href="css/article.css">
<div class="article-container">
<form action="search.php">
<input type="text" name="search" placeholder="Search">
<button type="submit" name="submit-search">Get answers</button>
</form>
<?php
$sql = "SELECT * FROM article";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div class='article-box'>
<h3>".$row['a_title']."</h3>
<p>".$row['a_text']."</p>
<hp>".$row['a_date']."</p>
<p>".$row['a_author']."</p>
</div>";
}
}
?>
Search.php "Where the search information should appear after click on 'Get answers button'"
<?php
include 'connect.php';
?>
<link rel="stylesheet" type="text/css" href="css/article.css">
<h1>Search Page</h1>
<div class="article-container">
<?php
if (isset($_POST['submit-search'])){
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM article WHERE a_title LIKE '%$search%' OR a_text
LIKE '%$search%' OR a_author LIKE '%$search%' OR a_date LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
echo "There are ".$queryResult." results!";
if($queryResult > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div class='article-box'>
<h3>".$row['a_title']."</h3>
<p>".$row['a_text']."</p>
<hp>".$row['a_date']."</p>
<p>".$row['a_author']."</p>
</div>";
}
}else{
echo "There are no results matching your search! Contact our support so
we can add this or if you have a result, add it, as a result, using the
GIVE ANSWER button!";
}
}
?>
</div>
You are using $_POST['search'] Which is wrong. Because your search string is submitted in get method. So you must use:-
$_GET['search']
Please try this.
I am a beginner at programming. I am learning PHP. What i am trying to do is write a search script in PHP for my project. When i try searching through MySQL database it gives me an error:
Notice: Undefined variable: output in C:\xampp\htdocs\search.php on line 2
I have checked everything on the script and cant see the problem. Have i coded it wrong?
i have checked all possible questions on the forum that relate to my question and they dont seem to gime me the answer i need. please help.
this is the HTML script with the input:
<form action="search.php" method="post" id="search">
<div id="searchfield_div">
<input name="search" id="searchfield" type="text" placeholder="What you looking for?">
<input id="delete_search_button" name="delete button" type="button" value="X">
<input id="search_button" name="search button" type="submit" value="Search">
</div>
</form>
and this is my PHP script:
<?php
//connection script
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "liquihub";
$output = '';
#mysqli_connect("$db_host","$db_user","$db_pass","$db_name") or die("could not connect");
//collection script
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","", $searchq);
$query = mysql_query("SELECT * FROM beverage_db WHERE name LIKE '%$searchq%' OR price LIKE '%$searchq%' OR type LIKE '%$searchq%'") or die ("could not search" );
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'We not stocking this particular item at present';
}else{
while($row = mysql_fetch_array($query)) {
$bevname = $row['name'];
$bevprice = $row['price'];
$bevtype = $row['type'];
$bevid = $row['id'];
$output .= '<div>'.$bevname.' '.$bevprice.' '.$bevtype.'</div>';
}
}
}
?>
The output script that's meant to put the results on a different page:
<?php print("$output");?>
You have no problem, what you are getting is a Warning not an Error. Your question is duplicated. Here.
I have a form where people can search the database for a certain user. When they search for the user and click submit, they're re-directed to a different page and the results are displayed.
My only issue is that the results are being displayed before the required html tags - here's an example of what the page looks like through Inspect Element:
"Bobby123
"
<!DOCTYPE html>
<html>
<body>
</body>
</html>
How do I display the results AFTER the required html tags? How do I set a "set place" for the results to be displayed?
Here's my code:
<?php
if(isset($_POST['submit'])) {
$term = $_POST['search'];
$searchuser = $stmt = $con->prepare("SELECT * FROM users WHERE username LIKE :term");
$stmt->bindValue(':term', '%'.$term.'%');
$stmt->execute();
if($searchuser->rowCount() > 0) {
while($row = $searchuser->fetch()){
$name = $row['username'];
echo $name;
}
}else{
echo 'No results';
}
}
?>
<form method="post" action="results.php">
<input name="search" type="search">
<input type="submit" name="submit">
</form>
The code on results.php simply is:
<!DOCTYPE html>
<html>
<body>
</html>
If possible, I would not like to use coding like Javascript, Jquery, or anything that is run on the client side.
Instead of
if($searchuser->rowCount() > 0) {
while($row = $searchuser->fetch()){
$name = $row['username'];
echo $name;
}}else{
echo 'No results';
}
}
use
if($searchuser->rowCount() > 0) {
$content = "";
while($row = $searchuser->fetch()){
$content .= '<p>' . $row['username'] . '</p>';
}
}else{
$content = 'No results';
}
Then, in your HTML (where you want the text to display)
<HTML>
<BODY>
<?PHP echo $content; ?>
</BODY>
</HTML>
I am trying to allow admin side of my website to edit member detail.
I have created search for admin so s/he can search users via their name or surname,
and I want to have edit link for each search result that it will come back.
just to let you know I really don't know how to do it.
This is the code I have
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
<script src="jquery.js"></script>
<script type="text/javascript">
function serachq ()
{
var searchTxt= $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt}, function(output){
$("#output").html(output);
});
}
</script>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members..." onkeydown="serachq();"/>
<input type="submit" value=">>" />
<br/>
<br/>
<div id="output">
</div>
</body>
</html>
and this is PHP code
<?php require_once("db_connection.php"); ?>
<?php
$output="";
if (isset($_POST['searchVal']))
{
$searchq=$_POST['searchVal'];
$searchq= preg_replace("#[^0-9a-z]#i","",$searchq);
$query= mysql_query("SELECT * FROM member WHERE first_name LIKE '%$searchq%' OR last_name LIKE'%$searchq%'") or die ("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output="there was no serach results!";
}
else
{
while ($row= mysql_fetch_array($query))
{
$fname=$row ['first_name'];
$lname=$row['last_name'];
$id= $row['member_id'];
$output.='<div>'.$fname.' '.$lname.' '.$id.'Edit Subject </div>';
}
}
}
echo ($output);
?>
Really you need to write the question's better in the future. Specifically, if you are getting an error provide the error. If you expect a certian result, tell us what you get instead. That said the thing that stands out is:
<?php echo $query ['$id'];
Where I imagine you meant:
<?php echo $query[$id];
Here is that part of the code cleaned up:
while ($row = mysql_fetch_array($query))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$id = $row['member_id'];
$output .= "<div>{$fname} {$lname} {$id} Edit Subject</div>";
}
If i understand your question properly, you can do using session, put $searchq in session and when you come back you can make the query form session
if (isset($_POST['searchVal']) || isset($_SESSION['serachq'])) {
if (isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$_SESSION['serachq'] = $searchq;
} else {
$searchq = $_SESSION['serachq'];
}
}
or you can modify your link add-member.php?edit=<?php echo $query ['$id']; ?>&q=<?php echo urlencode()?>
and your back url should have the &q=<?php echo urlencode()?> part too then
if (isset($_POST['searchVal']) || isset($_GET['q'])) {
if (isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
} else {
$searchq = urldecode($_GET['q']);
}
}
`