I am trying to Select a row from a database table and display the variables in another PHP file. Right now I have to trying just one variable, but will change to outputting a table later on. The error is an Undefined Variable:output.
I have tried using session but it prints out the array and doesn't go all the way through. I really just want the variable so that I can put them into a table easier.
My store file, which shows a search bar and where I am trying to display the results, under the search bar.
<?php
require "header.php";
?>
<main>
<form action="includes/itemsearch.php" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo $output ?>
</main>
<?php
require "footer.php";
?>
My item search file:
<?php
if (isset($_GET['search-submit'])) {
require 'connect.php';
$item = $_GET['item'];
$output = "";
$sql = "SELECT * FROM Product";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
$pName =$row['Product_Name'];
$manufacture=$row['Manufacture'];
$quantity=$row['Quantity'];
$price=$row['Price'];
$description=$row['Description'];
echo"<p>$pName</p><br />";
}
header("Location: ../store.php?");
}
I currently get an undefined variable error.
use post method instead of get
<?php
if (isset($_POST['search-submit'])) {
}
?>
$output is undefined because you initialize it on click of submit button so <?php echo $output ?> is executing first then it gets initialized.
try
<?php
if (isset($_GET['search-submit'])){
echo $output;
}
?>
to get output just require search file in store.php
// store.php
<?php
require "header.php";
require "search.php";
?>
<main>
<form action="" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo $output ?>
</main>
<?php
require "footer.php";
?>
this is the final code
// search.php
<?php
if (isset($_GET['search-submit'])) {
require 'connect.php';
$item = $_GET['item'];
$output = "";
$sql = "SELECT * FROM Product";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
$pName =$row['Product_Name'];
$manufacture=$row['Manufacture'];
$quantity=$row['Quantity'];
$price=$row['Price'];
$description=$row['Description'];
$output .= "<p>$pName</p><br />";
}
}
?>
// store.php
<?php
require "header.php";
require "search.php";
?>
<main>
<form action="" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo isset($_GET['search-submit']) ? $output : '' ?>
</main>
<?php
require "footer.php";
?>
Related
So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances.
Here is my script:
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$count = mysqli_num_rows($query);
if($count != "1"){
$output = '<h2>No result found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s = $row['name'];
$output .= '<h2>Found: '.$s.'</h2><br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php echo $output; ?>
</body>
</html>
Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?
<?php
if ($count==0) {
echo '<input type="submit" name="return" value="Return">';
}
?>
If you want a much cleaner html code, do this:
<form method="POST" action="index.html">
<?php if ($count!= "1") : ?>
<input type="submit" name="return" value="Return">
<?php else : ?>
<!-- put your other button here -->
<?php endif; ?>
</form>
You can read more about escaping from HTML here.
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$results = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php if(0 < count($results)) ?>
<?php foreach($results AS $row) : ?>
<h2><?= $row['name'] ?></h2>
<?php endforeach; ?>
<?php else : ?>
<H2> No results found!</h2>
<?php endif; ?>
</body>
</html>
I have made a basic search engine and I try, to fetch the results, on the same page; moreover, the results have been retrieved and stored in the associative array, but the embedded code in HTML shows only one record, of the results. `
<?php
require('Configuration/config.php');
require('Configuration/db.php');
//If the user clicks, on the button search, the execute the query
if (isset($_POST['search_btn'])) {
$search_query = $_POST['search'];
//$search_query = htmlspecialchars($_POST['search']);
//Create the query.
$query = "SELECT * FROM The_primary_arkivum WHERE
Name = '$search_query' OR
Address = '$search_query' OR
Category = '$search_query' OR
Country = '$search_query' OR
State = '$search_query'";
//Get the results.
$results = mysqli_query($conn, $query);
//Fetch the data, of the result, to an array.
$search_results = mysqli_fetch_all($results, MYSQLI_ASSOC);
//var_dump($search_results);
//var_dump($search_query);
//Free result
mysqli_free_result($results);
//Close the connection
mysqli_close($conn);
}
?>
<?php include('included/header.php'); ?>
<body>
<div class = "header">
<h2>Search</h2>
</div>
<form method="post" action="search_index.php">
<div class="input-group">
<label>Search</label>
<input type="text" name="search" value="<?php echo $search; ?>">
</div>
<div class="input-group">
<button type="submit" class="btn" name="search_btn">Search</button>
</div>
<?php foreach($search_results as $search_result) : ?>
<div class="mySlides fade">
<?php echo $search_result['Name']?>
<?php echo $search_result['Address']?>
<?php echo $search_result['Country']?>
</div>
<?php endforeach; ?>
</form>
<?php include('included/footer.php'); ?>
`
Your echo statements in your form do not have ending semicolons ;. Try starting there.
<?php echo $search_result['Name'];?>
<?php echo $search_result['Address'];?>
<?php echo $search_result['Country'];?>
Database values not updating
Working on a web form here. I want to update user info in a database. Using AJAX to pull from a list and generate information. When “Update Information” is clicked, the database should update the given values. I am just testing it on “FirstName” for now. The Code runs and I get no errors, but my database is not updating.
Process.php:
<body>
<?php
if (!isset($_SESSION['email']))
Header ("Location:logout.php") ;
if(isset($_POST['submit'])){
$NewFirstName = trim($_POST['NewFirstName']);
$StudentPID = $_POST['ca'];
$sql = "update Student SET FirstName=".$NewFirstName."where StudentPID = ".$StudentPID."";
$PleaseUpdate = mysql_query($sql);
//echo "<script type='text/javascript'>alert('Data was successfully inserted')</script>";
}
?>
<form name="input" action="process.php" method="post">
Ajax Demo
<br/><br/>
Select a Student: <br/><br/>
<select name="ca" onchange="showDetail(this.value)">
<?php print GetCategory(); ?>
</select>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<?php ?>
<br />
<input type="submit" name = "submit" value="Update Information">
</form>
<br/><br/>
Logout
</body>
</html>
GetDetail.php:
<?php session_start(); //this must be the very first line on the php page, to register this page to use session variables
//if this is a page that requires login always perform this session verification
require_once "inc/sessionVerify.php";
require_once "dbconnect.php";
if (!isset($_SESSION['email']))
Header ("Location:logout.php") ;
$q = $_GET['q']; //get the values passed from this query string
$sql = "select * from Student where StudentPID = '".$q."'";
$result = $DB->GetAll($sql);
//display the result in a table
print '<br /><br /><span style="color:red">Data retrieved from database:</span><br/ >';
//get the rows
$i = 0; //This is used to remember how many checkboxed created in total
foreach ($result as $row)
{
//Now make the form
print( '<form action="getDetail.php" method="post">
First Name: <input type="text" id="NewFirstName" name="NewFirstName" value='.$row["FirstName"].'><br>
Last Name: <input type="text" id="" name="" value='.$row["LastName"].'><br>
</form>
');
}
print '</table>';
?>
I'm trying to use a listbox form to query the database but it's not showing anything. The idea is that I've queried the database to fill the form with the names of suburbs and then, selecting a suburb will query the database again to return the names of parks in that suburb. When I use the search form it doesn't return anything.
this is the form:
<p>Select Suburb to search</p>
<form method="post" action="suburb_search.php" id="search">
<select>
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
<input type="submit" name="search" value="Search" />
</form>
</div>
This is where it should use the results of the form to query the database but its not working:
<?php
$searchRequest = False;
if (isset($_GET['suburb'])){
$search = $_GET['suburb'];
$sql2 = "SELECT * FROM park_list WHERE suburb=$search";
$result2 = $db->query($sql2);
if($message){
echo "<p>$message</>";
} else {
?>
<div class="form">
<?php
while ($row2 = $result2->fetch_assoc()){
?>
<div class="results">
<h2><?php echo $row2['park_name'];?></h2>
<?php
}
}
} ?>
give a name for your select element as
<select name="suburb">
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
and you are giving form method as POST but accepting data in GET in your php code, change this
if (isset($_GET['suburb'])){
to
if (isset($_POST['suburb'])){
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php') ?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
<input type="checkbox" name="list[]" value="<?php echo "$f6";?>" /><?php echo "$f6","<br>"; ?>//display result in htmlpage
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
print.php
//after submitting in php page
<?php include('connection.php') ?>
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected){
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem
I want to display content of checkbox from html form to php page.I extracted the checkbox contents from database.problem is how I display the content in php page selecting multiple checkbox.
First of all add error_reporting in your code:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
You have few issues in your HTML/PHP combination:
Issues:
You are using <input type="checkbox" .. inside the PHP.
your concatenation is wrong "$f6","<br>"
Also need to add name for like name="submit" in button.
Modified Code:
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?><br/>
<?php
$i++;
$d = $f6;
}
?>
Side note:
Suggestion of error_reporting is only for development and staging not for production.
Please use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.
<?php echo "$f6","<br>"; ?>
wrong concatenation
<?php echo "$f6"."<br>"; ?>
use this
Your code should be:-
<?php
// This line should be first.
// You have missed semicolon here.
include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<form action="print.php" method="post">
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<!-- You should write below line as -->
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?> </br>
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
there are couple of errors in your codes. please find the modified codes below with my comment started with //seeyouu:
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_num_rows($sql);//seeyouu: used wrong function>>mysql_numrows
$i=0; //seeyouu: after my amendment, probably can remove this
while ($row = mysql_fetch_array($sql)) {
//seeyouu: no such way of doing, your $sql already a result >> $f6=mysql_result($sql,$i,"item_name");
//seeyouu: forgot to close your php tag here
?>
<input type="checkbox" name="list[]" value="<?php echo $row["item_name"]; ?>" />
<?php //seeyouu: wrong>>echo "$f6","<br>";
//correct way:
echo $row["item_name"]."<br />";
//$d=$f6;i don't know what is this line for, so i leave it to you.
}
?>
<input type="submit" name="submit" value="submit"/> <!-- seeyouu: forgot to set name: submit -->
</form>
print.php
//after submitting in php page
<?php include('connection.php');
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected)
{
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem