PHP Database Filter - php

Im trying to have a simple select box that filters the database. I have successfully filled the database with all vallues on pageload, however I cant seem to get over the final hurdle of applying the filter. Also, once I have clicked the filter button, is it possible for the select text to stay at the text rather than returning to the default text?
[NOTE: I have pretty URL's enabled hence the no .php at the end]
The code is as followed:
<?php
$query = "SELECT * FROM Events";
echo $Type;
if (isset($_POST['filter'])) {
$Type = $_POST['value'];
$query .= " WHERE Type = '{$Type}'";
}
$result = mysql_query($query);
?>
<form action='/events' method="post" name="form_filter" >
<select class="eventList">
<option value="EVENT1">EVENT1</option>
<option value="EVENT2">EVENT2</option>
<option value="EVENT3">EVENT3</option>
<option value="EVENT4">EVENT4</option>
<option value="EVENT5">EVENT5</option>
<option value="EVENT6">EVENT6</option>
<input type="submit" name="filter" value="Filter">
</select>
</form>
<table class="table table-striped table-hover table-curved">
<thead>
<tr>
<th><b>Date</b></th>
<th><b>Event Name</b></th>
<th><b>Type</b></th>
<th><b>Region</b></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $Date = $row['Date']; ?></td>
<td><?php echo $Name = $row['Name']; ?></td>
<td><?php echo $Type = $row['Type']; ?></td>
<td><?php echo $Region = $row['Region']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

Your <select> does not have a name:
<select class="eventList" name="event">
Use that name as index in the $_POST array:
$Type = $_POST['event'];

Update your code as follow
<?php
$query = "SELECT * FROM Events";
echo $Type;
if (isset($_POST)) {
$Type = $_POST['event'];
$query .= " WHERE Type = '{$Type}'";
}
$result = mysql_query($query);
?>
<form action='/events' method="post" name="form_filter" >
<select class="eventList" name="event">
<option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>
<option value="EVENT2" <?php echo ($Type=="EVENT2")?'selected="selected"':'';?>>EVENT2</option>
<option value="EVENT3" <?php echo ($Type=="EVENT3")?'selected="selected"':'';?>>EVENT3</option>
<option value="EVENT4" <?php echo ($Type=="EVENT4")?'selected="selected"':'';?>>EVENT4</option>
<option value="EVENT5" <?php echo ($Type=="EVENT5")?'selected="selected"':'';?>>EVENT5</option>
<option value="EVENT6" <?php echo ($Type=="EVENT6")?'selected="selected"':'';?>>EVENT6</option>
<input type="submit" name="filter" value="Filter">
</select>
</form>
<table class="table table-striped table-hover table-curved">
<thead>
<tr>
<th><b>Date</b></th>
<th><b>Event Name</b></th>
<th><b>Type</b></th>
<th><b>Region</b></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $Date = $row['Date']; ?></td>
<td><?php echo $Name = $row['Name']; ?></td>
<td><?php echo $Type = $row['Type']; ?></td>
<td><?php echo $Region = $row['Region']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
i have update this line
`if (isset($_POST)) {
and added the select name and code list code
$Type =$_POST['event'];
<select class="eventList" name="event">
<option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>

Related

I want the php results to be displayed on the same page instead of a completely new page

I'm trying to search by name and display the results on the same page.
My php code and the html code are in 2 separate files. Given below is my search.php code.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
Given below is the html code for my form.
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
When I search, the results are displayed on a completely new page. But I want the results to be displayed on the same page with all the existing page layout, headers and footers.
How can I achieve this?
Thank in Advance!
Assuming your first file name is index.php.
Then need to call this file on form action & little bit refactor a file. Now it have $output array which is empty. If it contains search parameter, it will fill data to $output array and then on html part it checks if $output array has data to display it.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>

Cannot generate the report

I want to generate the report that click and choose from the category drop down list, then it will show that the order which product in the category that you select and how many of them sold. But it cannot come out, I have been find many times the errors, and I also tried the query in SQL as it showed correct. I don't know why. Please help me.
<?php
session_start();
include_once 'header_admin.php';
?>
<div class="report">
<h2>Sales Report</h2>
<div class="sales_report">
<form action="" method="POST">
<select name="category" class="category">
<option>Category</option>
<?php
include_once 'includes/dbh.php';
$query1 = "SELECT * from category";
$result1 = mysqli_query($conn, $query1);
while ($row1 = mysqli_fetch_assoc($result1)) {
$CategoryID = $row1['CategoryID'];
$rowData1 = $row1['CategoryName'];
?>
<option value="<?php echo $CategoryID; ?>"><?php echo $rowData1; ?></option>
<?php
}
?>
</select>
<button class="generate" type="submit" name="generate">Generate</button>
<table class="genreport">
<tr>
<th>Furniture ID</th>
<th>Furniture Name</th>
<th>Category Name</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total Amount</th>
</tr>
<?php
if (isset($_POST['generate'])) {
$catName = mysqli_real_escape_string($conn, $_POST['category']);
$query2 = "SELECT D.FurCode, F.FurName, C.CategoryName, O.OrderDate, D.quantity, COUNT(D.quantity * D.PriceEach)
FROM orderdetail D, furniture F, category C, ordertab O
WHERE C.CategoryName = '$catName' GROUP BY C.$catName ORDER BY C.$catName ";
$result2 = mysqli_query($conn, $query2);
if ($result2) {
while ($row2 = mysqli_fetch_array($result2)) {
$furCode = $row2['FurCode'];
$furName = $row2['FurName'];
$orderDate = $row2['OrderDate'];
$quantity = $row2['quantity'];
$priceEach = $row2['PriceEach'];
$total = $quantity * $priceEach;
?>
<tr>
<td><?php echo $furCode; ?></td>
<td><?php echo $furName; ?></td>
<td><?php echo $catName; ?></td>
<td><?php echo $orderDate; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $total; ?></td>
</tr>
<?php
}
}
} else {
echo '<tr><td>No Result!</td><td></td><td></td><td></td><td></td><td></td></tr>';
}
?>
</table>
</form>
</div>
</div>
</div>
</section>
</body>
</html>

Nested while loop is only running once

I'm running two while loops to display a table in html.
The first while loop is to display data from a selected databasetable as long as there is content in the database.
My second while loop displays a dropdown, where the content of another table should be displayed.
My goal is to show this dropdown each row. My problem is that the dropdown filled with data is just shown in the first tablerow. All the other rows just show an empty selectfield. Can someone help me what I did wrong?
My Code so far:
$link=pg_connect($conn_str);
if (!$link) {die('Verbindung schlug fehl.');}
$arbeitspaket = pg_query($link, "SELECT * FROM arbeitspaket WHERE id='$_SESSION[user_id]'");
$sql = "SELECT * FROM anwender ORDER BY nachname ASC";
$mitarbeiter = pg_query($link, $sql); ?>
<form action=mitarbeiterauswahl.php method=post>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php while($results=pg_fetch_array($arbeitspaket)){?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php while($row = pg_fetch_array($mitarbeiter)){
echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
</form>
Why don't you try a foreach? Looks much better in this situation. Something like this:
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php $results= pg_fetch_array($arbeitspaket);
$arbeiters = pg_fetch_array($mitarbeiter);
foreach($results as $result){?>
<tr>
<td><?php echo $result['apid']; ?></td>
<td><?php echo $result['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $result['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php foreach($arbeiters as $arbeiter) {
echo '<option value="'. $arbeiter) ['id'] .'">('. $arbeiter) ['id'] .') '. $arbeiter) ['vorname'] .' '. $arbeiter) ['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
The problem in your code is that you consume all the data in the first loop, after that it is gone. Store the data in an array and iterate over the array.
<form action=mitarbeiterauswahl.php method=post>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php
$mitarbeiter_array = array();
while($row = pg_fetch_array($mitarbeiter)) {
$mitarbeiter_array[] = $row;
}
unset($row);
?>
<?php while($results=pg_fetch_array($arbeitspaket)){?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php foreach($mitarbeiter_array as $row){
echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
</form>
i think it will be better way and will be fast
<form action=mitarbeiterauswahl.php method=post>
<?php
$select_html = '<select name="mitarbeiter">';
$select_list_data = pg_fetch_array($mitarbeiter);
?>
<?php foreach($select_list_data as $row){ ?>
<?php
$select_html .= '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>';
?>
<?php } ?>
<?php $select_html .= '</select>'; ?>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php
$fetch_results = pg_fetch_array($arbeitspaket);
foreach($fetch_results as $results){ ?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<?php echo $select_html; ?>
</td>
</tr>
<?php } ?>
</table>

Having issues sorting with links in PHP

So, I am trying to sort a database by clickable links. I have a function that is supposed to order them, but for some reason it isn't working. Also, once I add something to the database and submit, if I click on the links to order, it sends me to my actual function holding file in the url, which is weird.
Forgive table formatting issues. I'm still debugging that.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Project 1</title>
</head>
<body>
<h1 style='text-align:center;'>Movie Collection Database</h1>
<form method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align='center' border='1' cellpadding='5'>
<tr>
<td>
<input type="text" size='40' name="movieTitle" value="Movie Title">
</td>
<td>
<input type="text" name="studioName" value="Studio Name">
</td>
<td>
<select name="movieRating">
<option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option>
<option value="Not Rated">Not Rated</option>
</select>
</td>
<td>
<input type="text" name="publicationYear" value="2015">
</td>
<td>
<input type="number" name="imdbRating" value="10.0">
</td>
<td>
<input type="number" name="runTime" value="0">
</td>
<td>
<input type="checkbox" name="add">Add
</td>
</tr>
</table>
<?php
$db = new PDO("mysql:host=localhost;dbname=berkleyr", "berkleyr", "12345");
?>
<?php include_once("Project1DB.php"); ?>
<?php if ($_SERVER['REQUEST_METHOD'] === "POST"):
delete($db);
if(isset($_POST['add']))
{
try
{
Insert($db, $_POST['movieTitle'], $_POST['studioName'], $_POST['movieRating'], $_POST['publicationYear'], $_POST['imdbRating'], $_POST['runTime']);
}
catch (PDOException $error)
{
$db->rollback();
echo "Bad things Happened";
$db = null;
die("Connection failed: " . $error->getMessage());
}
}
?>
<table align='center' border='1'>
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php
if (isset($db))
{
$db = null; // make sure an exception didn't already close the connection. If not, close it now.
}
?>
<?php else: ?>
<table align='center' border="1">
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] ."' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php endif ?>
<td colspan="7" align="center">
<input type='submit' name='submit' value='Update Database' />
</td>
</table>
</form>
</body>
</html>
Here is my function for sorting:
function orderBy($order)
{
switch($order)
{
case "title":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.title ASC");
break;
case "studio":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.studio ASC");
break;
case "rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.rating ASC");
break;
case "pub_year":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.pub_year ASC");
break;
case "imdb_rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.imdb_rating ASC");
break;
case "run_time":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.run_time ASC");
break;
}
$sorted->execute();
}

Website search functionality with PHP

I'm a beginner and I try to make an agriculture application, to gain some experience, and to try something advanced. But I can't figure out how to make searches within my application. I have:
- 4 select html elements
- 5 search boxes
For space reasons, I'll write just 3/14 columns for the html elements.
HTML code:
<form>
<!-- makes the global filter based on all the data from db -->
<select id="select1" name="select1" onchange="this.form.submit()">
<option value="">Select</option>
<option value="code">Code</option>
<option value="land">Land</option>
<option value="name">Name</option>
</select><br/>
<!-- makes a filter for a html column, based on a db column -->
<select id="select2" name="select2" onchange="this.form.submit()">
<option value="">All parcels</option>
<option value="P1">P1</option>
<option value="P2">P2</option>
<option value="P3">P3</option>
</select>
<!-- search boxes to filter 5 html columns -->
<input type="search" id="search1" name="search2">
<input type="search" id="search2" name="search2">
<input type="search" id="search3" name="search3">
<input type="search" id="search4" name="search4">
<!-- same as the previous select menu, just this is for another html column -->
<select id="select3" name="select3" onchange="this.form.submit()">
<option value="">Select</option>
<option value="MO">MO</option>
<option value="MA">MA</option>
<option value="ME">ME</option>
</select>
<!-- the 5th search box -->
<input type="search" id="search5" name="search5">
<!-- renders the data from db according to the selections made by the user or search terms -->
<table id="table" class="display" cellspacing="0" width="100%">
<tr>
<th id="code">Code</th>
<th id="land">Land</th>
<th id="name">Name</th>
</tr>
<tr>
<?php
require 'config.php'; # login to database
# ---- QUERY ----
?>
</tr>
</table>
<!-- this displays in the page as many rows as the user selects -->
<select name="select4" id="select4" onchange="this.form.submit()">
<option value="">Select</option>
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
</select>
</form>
PHP code: I just put this block of code instead of "# ---- QUERY ----" from above, to populate the html columns within the page:
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch($select1){
case 'code':
$stmt = $db->query("SELECT * FROM users WHERE `code`='code'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # some more cases follows
} # end switch($select1)
} # end if(isset($_POST['select1']))
elseif(isset($_POST['select2'])){
$select2 = $_POST['select2'];
switch($select2){
case P1:
$stmt = $db->query("SELECT * FROM users WHERE `parcel` LIKE '%P1%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # some more cases here
} # end switch($select2)
} # end if(isset$_POST['select2'])
elseif(isset($_POST['search1'])){
$search1 = $_POST['search1'];
$stmt = $db->query("SELECT * FROM users WHERE `name` LIKE '%search1%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search1']))
elseif(isset($_POST['search2'])){
$search2 = $_POST['search2'];
$stmt = $db->query("SELECT * FROM users WHERE `cont_ref_a` LIKE '%search2%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search2']))
elseif(isset($_POST['search3'])){
$search3 = $_POST['search3'];
$stmt = $db->query("SELECT * FROM users WHERE `owner` LIKE '%search3%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search3']))
elseif(isset($_POST['search4'])){
$search4 = $_POST['search4'];
$stmt = $db->query("SELECT * FROM users WHERE `block` LIKE '%search4%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search4']))
elseif(isset($_POST['select3'])){
$select3 = $_POST['select3'];
switch($select3){
case 'T1':
$stmt = $db->query("SELECT * FROM users WHERE `zone`='T1'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # end case 'T1', more cases follows
} # end switch($select3)
} # end elseif(isset($_POST['select3']))
elseif (isset($_POST['search5'])){
$search5 = $_POST['search5'];
$stmt = $db->query("SELECT * FROM users WHERE `s_a` LIKE '%search5%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
} # end elseif(isset($_POST['search5']))
elseif(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch($select1){
case 'code':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
</tr><?php
}
break; # end case 'code', more cases follows
} # end switch($select1)
} # end elseif(isset($_POST[select1]))
elseif(isset($_POST['select4'])){
$select4 = $_POST['select4'];
switch($select4){
case 1:
$stmt = $db->query("SELECT * FROM users LIMIT 1");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
break; # end case 1, more cases follows
} # end switch($select4)
} # end elseif(isset($_POST['select4'])))
else { # if noone of the select menus or search boxes where filled, then just render all the data from db to the page
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
} # end php script
Sorry for this post beeing so long, but I tried to express as better as I can. The problem is that when I select one option from a select menu, or I enter a term to search, nothing is displayed. I don't care for now if something is missing in my code when comes about security, I just want to make this app work. Any help? Thanks a lot!
LE: I finally found the fix for my problem (and I will write it here so that any other persons that will search for this type of problems, to have an answer (I'll write a trivial example)):
For search-boxes:
<form action="" method="post">
<input type="search" name="search1" onchange="this.form.submit()">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr><?php
if(isset($_POST['search1'])){
$search1 = $_POST['search1'];
if(!empty($_POST['search1'])){
$sql = "SELECT * FROM tblName WHERE fname LIKE '%$search1%'";
$stmt = $db->query($sql);
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
}
}
else {
$sql = "SELECT * FROM tblName";
$stmt = $db->query($sql);
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
} ?>
</table>
</form>
For select-menus:
<form action="" method="post">
<select name="select1" onchange="this.form.submit()">
<option value="">Select</option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
</select>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr><?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
if(!empty($_POST['select1'])){
switch($select1){
case 'fname':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
</tr><?php
}
break;
case 'lname':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
break;
}
}
}
else {
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
}?>
</table>
</form>
Your sample code is incomplete. The way it is written, many of your search options won't work. I would try selecting select2 and P1. That should work.
But there is potentially a bug in all of your IF statements. You shouldn't just test if a $_POST variable is set, you should make sure it's not NULL or zero length (strlen($var) > 0). Post variables can be "set" but still not have been submitted with actual data.
I would removed everything in your example that isn't related to select2 and see if you can get it to work in a much simpler format.
And of course smashing all of your SQL and PHP code in with a bunch of HTML is a bad idea, but that's not part of the question.

Categories