Use Select option as search with dynamic fields - php

I am making a site linked with a database. It sorts transactions for budgeting reasons. For the store search section, I have a select drop down menu. I have linked up the data so that the drop down only shows stores that are in the database and automatically adds them as they change dynamically.
My issue is that I need a way to actually use the select options as search terms. Here is the initial page.
<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
Search Store:<br/>
<input type="text" name="StoreName">
<br/>
<input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};
$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};
$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};
echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";
};
echo '</select>'."\n";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is the output page.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
I can get regular typing search to work but I can't think of a way to search using the same method. Is it possible?

Related

How to display a list of users and search for a specific user at the same time?

This code is correct, but I want it to show me the user list after logging in, and bring me users with the same profile under each column I wanted to search for a specific user.
I have 20 items in the code, but I have written only 2 items here.
.usersearch.php
require_once 'users.php';
<table class="table table-bordered table-striped table-hover stara">
<thead>
<tr>
<th>آیدی</th>
<th>نام</th>
<tr>
<tr>
<th>
<form action="usersearch.php" method="post" novalidate>
<input name="searchid" >
</form>
</th>
<th>
<form action="/usersearch.php" method="post" novalidate>
<input name="searchname">
</form>
</th>
<tr>
<?php while($user = $result-> fetch(PDO::FETCH_ASSOC )) { ?>
<tr class="bgyellow">
<th>
<?php echo $user['id'] ; ?>
</th>
<th>
<?php echo $user['name'] ; ?>
</th>
<th>
</tr>
<?php } ?>
</thead>
<tbody >
<?php while($row=$select_stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td> <?php echo $row['id'] ; ?> </td>
<td> <?php echo $row['name'] ; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
.users.php
if(isset($_SESSION['user']) ) {
$db = new PDO("mysql:host=localhost;dbname=pasak", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select_stmt = $db->prepare("SELECT * FROM users");
$select_stmt->execute();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_POST['searchid']){
$id=$_POST['searchid'];
$result = $db->prepare( "SELECT * FROM users WHERE id =:id");
$result->execute((compact('id')));
if($result ->rowCount() <1){
echo 'چنین کاربری وجود ندارد';
}
echo "</br>"; echo "</br>";
}
if($_POST['searchname']){
$name=$_POST['searchname'];
$result = $db->prepare( "SELECT * FROM users WHERE name =:name");
$result->execute((compact('name')));
if($result ->rowCount() <1){
echo 'چنین کاربری وجود ندارد';
}
echo "</br>"; echo "</br>";
}
}

DATATABLE plugin Fetch only first row only in dynamic data

<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0" >
<thead>
<tr class=" card-header py-2" style=" text-align:center;">
<th> Er no</th>
<th> Name</th>
<th> Branch</th>
<th> Adm Year</th>
<th>Profile</th>
</tr>
<?php
if (isset($_POST['pass'])) {
$d = $_POST['spass'];
include('bcpdb.php');
$iq = "select * from stuinfo where ERNO like '%$d' ";
$qs = mysqli_query($con, $iq);
//echo "Serch Query Data";
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
} else {
include('bcpdb.php');
$iq = "select * from stuinfo ";
$qs = mysqli_query($con, $iq);
//echo "select Query Data";
}
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
while ($res = mysqli_fetch_assoc($qs)) {
?>
<tbody>
<tr>
<td> <?php echo $res['ERNO']; ?></td>
<td> <?php echo $res['SNAME']; ?></td>
<td><?php echo $res['BRANCH']; ?></td>
<td> <?php echo $res['ADMYEAR']; ?></td>
<td style="text-align:center;">
</td>
<?php
}
?>
</tr>
</tbody>
</table>
Just show rows in page not fetch all rows in datatable. I want all of the data of database fetch in datatable plugin, when I put some data in html table row column then it works perfectly, fetch and search all rows of data but in database data which fetch by select query, fetch only one row
[1]: https://i.stack.imgur.com/44FRd.jpg

Image is uploaded but not displayed in php

I had this project wherein, I am supposed to insert and select pictures, as in upload and view them. Initially, when I tried a code, it worked but the image didn't display. After many trials, I just copied a code from a mate which worked for him but when I tried, it didn't. Why? I have posted everything below-
Image upload code
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap");
$query = "SELECT * FROM `blobclob`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Image View Code
<html>
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap_exp");
$query = "SELECT * FROM `images`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
Database
it should be something like this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
it should be **src="data:image/jpeg;base64 ... ** instead of src="data:image..

php mysql database get price total [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I have using php code with mysql database showing data into html table, and i'm using filter on listview, now I'm stack with get price Total of the list after filtered. I'm not good enough with php code, am I made mistake with php or sql?
MySQL table:
id INT -
name VARCHAR -
code VARCHAR -
price INT
here is my code so far:
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `toy` WHERE CONCAT(`id`, `name`, `code`, `price`)
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `toy` LIMIT 5";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "user", "pass",
"database");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Wu</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="toys-grid">
<div class="search-box">
<br>
<form action="index.php" method="post">
<input type="text" name="valueToSearch" placeholder="Name To Search">
<br><br>
<input type="submit" name="search" value="Filter"><br><br>
</div>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
$results = mysql_query('SELECT SUM(price) FROM toy');
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
echo $rows['SUM(price)'];
}
?>
</th>
</tr>
</thead>
</table>
</form>
</div>
</body>
I appreciate your help...
No need to execute query for getting sum. You can do it with php
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php $sum = 0; ?>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
<?php
$sum += $row['price'];
?>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
echo $sum;
?>
</th>
</tr>
</thead>
</table>

perform multiple sql queries when clicking a submit button on PHP Page

I have a PHP page, that on clicking the submit button process a few MySQL queries.
in MySQL PHPMyAdmin the query works 100% and both queries execute. However, when in my PHP Code the queries do not execute.
Any help would be appreciated, I bet this is a simple one for decent PHP coders.
Thanks in advance Ryan.
My Code is:
<?php
mysql_connect("localhost", "hulamin_hulamin", "Hulamin2011")or die("cannot connect");
mysql_select_db("hulamin_loc")or die("cannot select DB");
$sql="SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>
<table border=0>
<tr>
<td>
<form name="form1" method="post">
<table border=1>
<tr>
<td width=150>Dispatch Area</td>
<td width=300>Customer</td>
<td width=150>Case Number</td>
<td width=100>Weight</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['dispatcharea']; ?></td>
<td><?php echo $rows['customer']; ?></td>
<td><?php echo $rows['casenumber']; ?></td>
<td><?php echo $rows['weight']; ?></td>
</tr>
<?php
}
?>
</table>
<input name="Next" type="submit" id="Next" value="Next">
<?php
if($_REQUEST['Next']=='Next') {
{
$sql="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0; update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0;";
$final=mysql_query($sql);
if($final)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
}
}
}
?>
</table>
</form>
</td>
</tr>
</table>
Thanks again,
Ryan
According to the PHP docs, mysql_query doesn't support multiple queries. PHPMyAdmin is probably separating the queries before executing them. Try splitting your query into two parts. (Also, the PHP docs say that you shouldn't end mysql_queries in semicolons, but it doesn't appear to hurt.)
I think that this is likely to be your problem
while($rows=mysql_fetch_array($result)){
Should be
while($rows=mysql_fetch_assoc($result)){
To refer to them as
echo $rows['dispatcharea'];
EDIT:
You also need to split both of your queries up in to two separate queries, because you cannot run two queries in one mysqli_query() tag.
You will need to split them as shown below:
// First update query
$sql1="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0";
// Second update query
$sql2="update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0";
// Run both queries independently
$final_query1 = mysql_query($sql1);
$final_query2 = mysql_query($sql2);
// Check for query success
if ($final_query1 && $final_query2)
{
// Success running the queries
}
else
{
// Unsuccessful running the queries
}
If you want to execute multiple queries at once, you can change over to using mysqli functions.
There is a mysqli function mysqli_multi_query() that can be used to execute multiple queries at once.
Please refer to:
http://php.net/manual/en/mysqli.multi-query.php
Here's a rough rewrite of the code using mysqli_multi_query() in an object oriented style:
<?php
$link = mysqli_connect('localhost', 'hulamin_hulamin', 'Hulamin2011', 'hulamin_loc');
$sql = "SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";
$result = $link->query($sql);
$count = $result->num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>
<table border=0>
<tr>
<td>
<form name="form1" method="post">
<table border=1>
<tr>
<td width=150>Dispatch Area</td>
<td width=300>Customer</td>
<td width=150>Case Number</td>
<td width=100>Weight</td>
</tr>
<?php
while($rows = $link->fetch_array($result)){
?>
<tr>
<td><?php echo $rows['dispatcharea']; ?></td>
<td><?php echo $rows['customer']; ?></td>
<td><?php echo $rows['casenumber']; ?></td>
<td><?php echo $rows['weight']; ?></td>
</tr>
<?php
}
?>
</table>
<input name="Next" type="submit" id="Next" value="Next">
<?php
if($_REQUEST['Next']=='Next'){
{
$multi_sql = "update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0;";
$multi_sql .= "update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0";
$final = $link->multi_query($multi_sql);
if($final)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
} }
}
?>
</table>
</form>
</td>
</tr>
</table>
I thought I would just post my complete code that is now working. thanks to all who replied to my question, it is greatly appreciated. It is really awesome to be a part of this community that is so responsive.
My code is:
<?php
mysql_connect("localhost", "username", "password")or die("cannot connect");
mysql_select_db("dbname")or die("cannot select DB");
$sql="SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>
<table>
<tr>
<td>
<center>
Load Number
</td>
<td>
<center>
Number of Cases
</td>
<td>
<center>
Load Weight
</td>
<td>
<center>
Other Detail
</td>
</tr>
<tr>
<td width=150>
<center>
<?php
$loadid = mysql_query('SELECT max(loadid)+1 FROM loaddetails');
if (!$loadid) {
die('Could not query:' . mysql_error());
}
echo mysql_result($loadid, 0);
?>
</td>
<td width=150>
<center>
<?php
$nocases = mysql_query('SELECT count(*) FROM loaddetails where loadid = 0');
if (!$nocases) {
die('Could not query:' . mysql_error());
}
echo mysql_result($nocases, 0);
?>
</td>
<td width=150>
<center>
<?php
$weight = mysql_query('SELECT SUM(WEIGHT) FROM loaddetails where loadid = 0');
if (!$loadid) {
die('Could not query:' . mysql_error());
}
echo mysql_result($weight, 0);
?>
</td>
<td width=150>
<center>
</td>
</tr>
</table>
<hr>
<table border=0>
<tr>
<td>
<form name="form1" method="post">
<table border=1>
<tr>
<td width=150>Dispatch Area</td>
<td width=300>Customer</td>
<td width=150>Case Number</td>
<td width=100>Weight</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['dispatcharea']; ?></td>
<td><?php echo $rows['customer']; ?></td>
<td><?php echo $rows['casenumber']; ?></td>
<td><?php echo $rows['weight']; ?></td>
</tr>
<?php
}
?>
</table>
<input name="Next" type="submit" id="Next" value="Next">
<?php
// First update query
$sql1="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0";
// Second update query
$sql2="update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0";
if($_REQUEST['Next']=='Next'){
// Run both queries independently
$final_query1 = mysql_query($sql1);
$final_query2 = mysql_query($sql2);
if ($final_query1 && $final_query2)
{
// Success running the queries
echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
}
else
{
// Unsuccessful running the queries
}
}
?>
</table>
</form>
</td>
</tr>
</table>

Categories