How to submit row from one table to another using php, msqli - php

I have the following php search script.
<?php
$query = $_GET['query'];
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM florinvtable
WHERE (`Brand` LIKE '%".$query."%') OR (`Description` LIKE '%".$query."%') OR (`Category` LIKE '%".$query."%')
ORDER BY Category, Brand, Price") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<div style='position:relative; font-size:18px; width:500px;'><span style='font-size:14px'>".$results['Category']."</span> - <strong>".$results['Brand']."</strong> - ".$results['Description']." - <span style='color:red;'>$".$results['Price']."</span> ".$results['Size']."</div>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
I would like to incorporate a submit button for each row displayed that I can click on to copy a row to an identical separate table. You might ask, why maintain two identical tables? The answer is the original table is frequently truncated. That said, how would I incorporate a submit button into the above script using the script below.
<?php
$query = "INSERT INTO floradtable (Category, Brand, Price)
SELECT Category, Brand, Price FROM florinvtable
WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."'
LIMIT 0,1";
$mysqli->query($query);
?>
As a side note, I know I am mixing mysql with mysqli which I will address after I work out a solution for the current problem. Any help is appreciated.

Add this.
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
$query = "INSERT INTO floradtable (Category, Brand, Price) VALUES";
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
$query .= "('".$results['Category']."','".$results['Brand']."','".$results['Price']."'),";
echo "<div style='position:relative; font-size:18px; width:500px;'><span style='font-size:14px'>".$results['Category']."</span> - <strong>".$results['Brand']."</strong> - ".$results['Description']." - <span style='color:red;'>$".$results['Price']."</span> ".$results['Size']."</div>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
$query = substr($query, 0, -1);//to remove the trailing comma
$mysqli->query($query);
}

Related

How do i navigate through images stored in mysql rather than displaying all of them?

This is my code but it displays all of the images saved in the database, i just want one to be displayed and the rest by using next and previous buttons. any help would be appreciated. Thanks!
<?php
$sql = "SELECT * FROM images";
$result = mysqli_query($conn, $sql) or die("bad request: $sql");
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
if($i%3 == 0)
{
echo "<tr>";
}
echo"<td><img src='user_data/{$row['FILE_NAME']}' width=200 height=200></td>";
if($i%3 == 2) {
echo"</tr>";
}
}
?>
You should use LIMIT and OFFSET in your query. It would look like this:
$sql = "SELECT file_name FROM images LIMIT 0,1";
This will get you 1 record, starting at record 0. So from that query you can build your pagination script. Just keep adding one to the offset (the first number in the LIMIT clause). Remember to filter the page number (make sure it’s an int).

Display Image together with title and description as results from database using search bar

Need help to echo image together with the title and description in the code below, when search it display title and description that i insert in the database, i want to display image together with title and description when search in the search bar
<?php
$query = $_GET['query'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM products
WHERE (`title` LIKE '%".$query."%') OR (`description` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['title']."</h3>".$results['description']."</p>";
}
}
else{
echo "No results";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
As if I understand it right then the problem is you're not printing your image
while($results = mysql_fetch_array($raw_results)){
echo '<img src=".$result['image']." >';
echo "<p><h3>".$results['title']."</h3>".$results['description']."</p>";
}
Mysql api will be completely removed in PHP 7
And please use mysqli parametrized query or pdo See the bobby tables
http://bobby-tables.com/
With that, I think you need also to store the links of the images in the database. Then echo the path of the image.

php search query only outputs two results...not the entire row

Hi I have a search query in index.html..when the user presses search...it goes to searchjobs.php.
I would like it to output the entire row. Which is
job_id
job_title
job_description
job_location
job_category
Currently it's only outputting only two of those fields which is job_title and job _description. How do I make it output the entire row? For example if the person searches for 'Leeds' I would like it to output the entire row about the job in leeds.
I think the issue is in the echo area.
If I enter more variables in the php script...it won't run.
<?php
mysql_connect("*****", "root", "*****") or die("Error connecting to database: ".mysql_error());
/*
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("jobslist") or die(mysql_error());
/* tutorial_search is the name of database we've created */
$query = $_GET['query'];
// gets value sent over search form
$min_length = 4;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM jobs_list
WHERE (`job_title` LIKE '%".$query."%') OR (`job_location` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// jobs_list is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<p><h3>".$results['job_id']."</h3>"
.$results['job_title'].
.$results['job_description'].
.$results['job_location'].
.$results['job_category'].
"</p>";
// posts results gotten from database you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
You are using concatination dots on both sides of the variable, remove the extra dots:
echo '<p><h3>'.$results['job_id'].'</h3>'
. $results['job_title']
. $results['job_description']
. $results['job_location']
. $results['job_category']
. '</p>';
I assume you are currently testing and trying, otherwise you should structure your job description properly i.e.
$tplJob= '<div class="job_entry">
<h3>ID: %s</h3>
<div class="title">%s</div>
<div class="description">%s</div>
<div class="location">Location: %s</div>
<div class="category">Category: %s</div>
</div>';
// echo formatted output based on above template variable
echo sprintf($tplJob, $results['job_id'], $results['job_title'], $results['job_description'], $results['job_location'], $results['job_category']);

How to SUM, SUM of Total from three table

Sorry I would like to repeat the question since I think was not clear to you, Im doing this b'cose is difficult to me to get what I expect.
I want to get SUM of Total which i get from three table from single query, Here my tables!!
1: payment
id | school_fee | trans_fee
1 20000 3000
2 10000 2000
The total is 35000
and the codes is here
<?php
//mysql_connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT SUM(school_fee+trans_fee) As Total
FROM payment WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`,
`title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example
if
$query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
//
$results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show
id ($results['id'])
}{
echo " Total amount of money payed by "
.$results['class'] ." "."class is " . $results ['Total'] .
" /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
?>
2:payment_one
id | school_fee | trans_fee
1 10000 20000
2 30000 50000
The Total is 110000
The CODEs is the same with the first one table except name of the table
here the codes
<?php
//mysql_connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT SUM(school_fee+trans_fee) As Total
FROM payment_one WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`,
`title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example
if
$query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
//
$results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show
id ($results['id'])
}{
echo " Total amount of money payed by "
.$results['class'] ." "."class is " . $results ['Total'] .
" /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
?>
The last one is
3: payment_two
id | school_fee | trans_fee
1 10000 20000
2 30000 10000
The Total is 70000
Codes as follows
<?php
//mysql_connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT SUM(school_fee+trans_fee) As Total
FROM payment_two WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`,
`title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example
if
$query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
//
$results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show
id ($results['id'])
}{
echo " Total amount of money payed by "
.$results['class'] ." "."class is " . $results ['Total'] .
" /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
?>
So I want to query by php so that I may get 215000 as Grand total of those three table,
I need the help please.
NOTE: id is auto increament.
You could do that with UNION:
SELECT SUM(total) FROM
(
SELECT SUM(school_fee+trans_fee) As Total
FROM payment
UNION
SELECT SUM(school_fee+trans_fee) As Total
FROM payment_one
UNION
SELECT SUM(school_fee+trans_fee) As Total
FROM payment_two
) a
sqlfiddle demo

How to query SELECT SUM of Total from three different table php

Hope you good Guys! I have a big problem on SELECT three different table and SUM there Total so that I may get Grand total of those three tables.
The table as follows, I just mention some of fields:
1:payment
id idnumber school_fee trans_fee
1 va03 10000 20000
2:payment_one
id idnumber school_fee trans_fee
1 va01 10000 30000
3:payment_two
id idnumber school_fee trans_fee
1 va02 40000 50000
I have already get 'Total' from each table, what I want is to SUM UP those Total I get, to have Grand total from those three tables.
Here my php codes;
1:payment:
<?php
//include mysql connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT
*,SUM(school_fee+trans_fee)
As Total FROM payment
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
$raw_results2 = mysql_query("SELECT * FROM payment
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
if(mysql_num_rows($raw_results2) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
// $results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show id
($results['id'])
}{
echo " Total amount of money payed by " .$results['class']
." "."class is " . $results ['Total'] . " /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
}
?>
2:payment_one
<?php
//include mysql connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT
*,SUM(school_fee+trans_fee)
As Total FROM payment_one
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
$raw_results2 = mysql_query("SELECT * FROM payment_one
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
if(mysql_num_rows($raw_results2) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
// $results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show id
($results['id'])
}{
echo " Total amount of money payed by " .$results['class']
." "."class is " . $results ['Total'] . " /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
}
?>
3:payment_two
<?php
//include mysql connect
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT
*,SUM(school_fee+trans_fee)
As Total FROM payment_two
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
$raw_results2 = mysql_query("SELECT * FROM payment_two
WHERE (`class` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
if(mysql_num_rows($raw_results2) > 0){
// if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
// $results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
// posts results gotten from database(title and text) you can also show id
($results['id'])
}{
echo " Total amount of money payed by " .$results['class']
." "."class is " . $results ['Total'] . " /=Tshs";
echo"<br>"; echo"<br>";
}
}
}
}
}
?>
Any help I'will be thankfully.
If I understand correctly, all of your three tables [payment][payment_one][payment_two] have same columns: id, idnumber, school_fee trans_fee.
You would be able to use one single table instead, and distinguish them by introducing a new column: tablenum, then it will be simple to obtain what you want. Please note both [id] and [tablenum] are primary key (composite primary key) now.
New table schema and the data would be (I'm not very sure about the purpose of your idnumber column):
[payment]
id tablenum idnumber school_fee trans_fee
1 0 va03 10000 20000
1 1 va01 10000 30000
1 2 va02 40000 50000
SQL example:
SELECT
school_fee,
trans_fee,
(school_fee + trans_fee) as 'total'
FROM payment WHERE id=1

Categories