I'm just learning PHP and I followed the book to the last detail and when I go to the localhost web page that I created to pull information from the database nothing shows on the webpage. I've troubleshooted all night I've Google and read the book over and over to see what I missed. I'm posting the code that I wrote to see if someone can lead me in the right area to fix my problem.
<html>
<head>
<title>Pay Scale</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width:150px;
text-align: left;
}
</style>
</head>
<body>
<h1>Pay Scale</h1>
<form method= "post" action= "Payscale.php"
<input type="hidden" name="submitted" value="true" />
<label> Search Category:
<select name="category">
<option value="Id">ID</option>
<option value="First Name">First Name</option>
<option value="Last_Name">Last_Name</option>
</select>
</label> <label>Search Criteria:
<input type="text" name="criteria" />
</label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
include('payconnect.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM employee pay range WHERE $category = '$criteria'";
$result = mysqli_query($dbcon, $query) or die('error getting data');
echo "<table>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last_Name</th> <th>Pay_Range</th> </tr>";
While($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['ID'];
echo "</td><td>";
echo $row['First Name'];
echo "</td><td>";
echo $row['Last_Name'];
echo "</td><td>";
echo $row['Pay_Range'];
echo "</td><tr>";
}
echo "<table>";
}
?>
</body>
</html>
There are a couple of problems with the html code for the page. The most obvious is that there is no closing > on your form tag, so the browser slurps up the next line, thinking that it is part of the form tag. Since that line sets the $_POST['submitted'] variable that your PHP script is looking for, the value doesn't get set and the script never runs.
To correct the problem, just add a > to your form declaration:
<form method="post" action="Payscale.php">
There are also a few other problems in the HTML that could be fixed at the same time:
At the top of the page:
<html>
<head>
A document should start with a DOCTYPE declaration so that the browser knows how to interpret the page. For HTML5, this is as simple as adding a line to the start of your file:
<!DOCTYPE html>
<html>
<head>
There is also an error in the table creation code:
echo $row['Last_Name'];
echo "</td><td>";
echo $row['Pay_Range'];
echo "</td><tr>"; # A
}
echo "<table>"; # B
The lines marked A, and B create new elements--a table row, and a new table respectively. You should be closing the elements--i.e. using </tr> and </table>.
It appears that your opening tag is not closed (a minor syntactical error).
Try changing it from this:
<form method= "post" action= "Payscale.php"
to this:
<form method="post" action="Payscale.php">
and see if that solves the problem.
Try this it's working :
<html>
<head>
<title>Pay Scale</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width:150px;
text-align: left;
}
</style>
</head>
<body>
<h1>Pay Scale</h1>
<form method= "post" action="Payscale.php">
<input type="hidden" name="submit" value="true"/>
<label> Search Category:
<select name="category">
<option value="Id">ID</option>
<option value="First Name">First Name</option>
<option value="Last_Name">Last_Name</option>
</select>
</label> <label>Search Criteria:
<input type="text" name="criteria" />
</label>
<input type="submit" value="submit" name="submitted"/>
</form>
</body>
</html>
Payscale.php
<?php
if (isset($_POST['submitted'])){
include('payconnect.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM employee pay range WHERE $category = '$criteria'";
$result = mysqli_query($dbcon, $query) or die('error getting data');
echo "<table>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last_Name</th> <th>Pay_Range</th> </tr>";
While($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['ID'];
echo "</td><td>";
echo $row['First Name'];
echo "</td><td>";
echo $row['Last_Name'];
echo "</td><td>";
echo $row['Pay_Range'];
echo "</td><tr>";
}
echo "<table>";
}
?>
Related
Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.
However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.
The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode
<html>
<head>
<title> Logo Search</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width: 250px;
text-align: left;
}
</style>
</head>
<body>
<h1> National Logo Search</h1>
<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')
or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')
LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.
<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>
<label>Search Category:
<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.
In short it doesn't have any rule how to show.
Played around with it for a bit and found the answer. I needed to define the criteria. see below code
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')
Main objective for this post is to open up a can of worms. I would like to get some advise on what is the best way/ most efficient way to search a phpmyadmin database.
I am just starting to learn PHP. Main goal is to be able to write my own search databases as that has always interested me. I played around with some code to build a basic search database and have it working now.
Below is the code I used. I would guess as it is my first attempt at this I am going about it the wrong way. Assuming this was a large database, how could I make this more efficient? Am I going about it the wrong way using PHP? Is there a better way to do this?
The goal is to have searchable database where the user has multiple options to query to help refine the results.
<html>
<head>
<title> test Search </title>
<style type="text/css">
table {
background-color: #ffffff;
}
th {
width: 200px;
text-align: left;
}
</style>
</head>
<body>
<h1> test Search</h1>
<form method="post" action="index.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<label>State: <input type="text" name="criteria5" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')
LIMIT 0,10";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I have written the below code as a test , but results are not comng in under the table I have created , the table appear along the top and all the results appear down the left hand side and even if I dont add anythig in the search all teh results show.?, cant see where I am going wrong. I am not worried about the actual HTML Look as it just a test. Appreciate the help. Error is
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel= "stylesheet" href="style5.css"/>
</head>
<boby>
<div class="third_bar">
<div class="second_image">
</div>
<div class="form"><form action= "search1.php" method="post">
<input type="text" name="search" id="search_bar" placeholder="" value="Search for your whisky here" max length="30" autocomplete="off" onMouseDown="active();" onBlur="inactive();"/><input type="submit" id="search_button" value="Go!"/>
</form>
</div>
</body>
</div>
</html>
search1.php
<?php
if (isset($_POST['search'])){
include ('connect.php');
$search = $_POST['search'];
$query = "SELECT * FROM whisky_results";
$result = mysqli_query($conn, $query) or die ('error getting data');
echo "<table>";
echo "<tr> <th>Whisky Name</th> <th>Whisky Desription</th> <th>Highest Price Recorded</th> <th>Lowest Price Recorded</th> </tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo "<tr><td>";
echo $row['description'];
echo "</td><td>";
echo "<tr><td>";
echo $row['highest_price'];
echo "</td><td>";
echo "<tr><td>";
echo $row['lowest_price'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
I have created a html form that is populated by a mysql query. The data has spaces in it.
eg "Bob site", "Sarah home"
When the form is created in the browser I get this:
Bob
site
Sarah
home
Instead of
Bob site
Sarah home.
I know I need to format my data but can't find any examples. My form code is below.
<?php include("includes/header.php"); ?>
<?php
$site = mysql_query("select distinct `site_name` from sept_billing");
?>
<html>
<body>
<h2>Create a new install</h2>
<form method="post" action="actions/newmachine_action.php">
<table border = '0'>
<tr>
<td><b>Site name:</b></td>
<td><input id="site" type="text" name="site" size="40" list="sites" value = 'Choose site name' />
<datalist id="sites">
<?php
while ($row = mysql_fetch_array($site))
{
echo "<option value=".$row[0].">";
}
?>
</datalist></td>
</tr>
</table>
<input type="submit" value="Create" />
</form>
</body>
</html>
<?php include("includes/footer.php"); ?>
Please change this and let me know if this works
echo "<option value=".$row[0].">";
to
echo "<option value='".$row[0]."'>";
This will work
<?php
while ($row = mysql_fetch_array($site))
{ ?>
<option><?php echo $row['name']; ?></option>
<?php } ?>
try
"<option text='".$row[0]."' value='".preg_replace(' ', '-', $row[0])."'></option>"
or
"<option value='".preg_replace(' ', '-', $row[0])."'>".$row[0]."</option>"
i think that value cannot have spaces
What's going on here? Why won't it run? I've tried to look through the code for simple errors, and don't see any -- I've also looked through the forums. Nothing!
Is there something simple that I'm forgetting?
<html>
<head>
<title>Search</title>
<style type='text/css">
table {
background-color: #FCF;
}
th {
width: 150px;
text-align: left;
}
</style>
</head>
<body>
<h1>Search</h1>
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true" />
<label> Search | Category:
<select name="category">
<option value="date">Date</option>
<option value="name">Name</option>
<option value="account">Account</option>
<option value="notes">Notes</option>
</select>
</label>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to DB
include('connectdb.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM calls WHERE $category = '$category'";
$result = mysqli_query ($dbcon, $query) or die ('Error');
echo "<table>";
echo "<tr> <th>Date</th> <th>Name</th> <th>Account</th> <th>Notes</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row ['date'];
echo "<tr><td>";
echo $row ['name'];
echo "<tr><td>";
echo $row ['account'];
echo "<tr><td>";
echo $row ['notes'];
echo "</table>";
?>
</body></html>
}
You're missing ending curly brackets for both the if() statement and the while() statement.
There is a trailing curly bracket at the end of the code, but it is outside of the php tags.
You closed your PHP tags before closing your braces. This should work.
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row ['date'];
echo "<tr><td>";
echo $row ['name'];
echo "<tr><td>";
echo $row ['account'];
echo "<tr><td>";
echo $row ['notes'];
echo "</table>";
}
?>
</body></html>
Your last three lines are off. You have a close bracket not in a PHP tag. Look at your source code and that should be the last thing you see. That error is because PHP did not see your close bracket ending the if.