How to display search result on the same page(HTML,PHP,MYSQL)

Hi,

i’m new to php,html and mysql. i have been assign by my teacher to do the below website

i have a html page that call for php function and retrieve data from mysql. i did manage to do it somehow. But it bothers me when the result of the search is display on another blank page using my php.

So, now can i display the result on my search.html?

Below is my code

in my html

[code]

New Arrival Car For Sale Prestige No Plate
[/code]

In my php

[php]<?php

// Get the search variable from URL

$var = @$_GET[‘term’] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == “”)
{
echo “

Please enter a search…

”;
exit;
}

// check for a search parameter
if (!isset($var))
{
echo “

We dont seem to have a search parameter!

”;
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect(“localhost”,“root”,“shaun830511”); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db(“mysql”) or die(“Unable to select database”); //select which database we’re using

// Build SQL Query
$query = “select * from car where class like “%$trimmed%”
order by type”; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo “

Results

”;
echo “

Sorry, your search: “” . $trimmed . “” returned zero results

”;

// google
echo “

<a href=“http://www.google.com/search?q=”
. $trimmed . “” target=”_blank" title=“Look up
" . $trimmed . " on Google”>Click here to try the
search on google

";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die(“Couldn’t execute query”);

// display what the person searched for
echo “

You searched for: “” . $var . “”

”;

// begin to show results set
echo “Results”;
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
echo 'No: '.$row[‘id’];
echo '
type: '.$row[‘class’];
echo ’
category: '.$row[‘cat’];
echo ’
brand: '.$row[‘brand’];
echo ’
year: '.$row[‘year’];
echo ’
colour: '.$row[‘colour’];
echo ’
mileage: '.$row[‘mileage’];
echo ’
note: '.$row[‘note’];
echo ’
description: '.$row[‘descp’];
echo ’
path: ‘.$row[‘name’];
echo ‘






’;
echo “

”;
echo “";
echo ‘










’;
echo “
<img src='images/” .$row[‘name’]."’>
”;
echo “$count.) $title” ;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo “
”;

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href="$PHP_SELF?s=$prevs&q=$var"><<
Prev 10&nbsp ";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo " <a href="$PHP_SELF?s=$news&q=$var">Next 10 >>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo “

Showing results $b to $a of $numrows

”;

?>[/php]

May i know what is the option and how i going to make the result display on the same page as on my html. i have tough luck on getting it for hours. been look through many tutorial but still cant get it.

Please advice.

TQ

With how you have it, you’ll have to rename the page to search.php. If you’re not allowed to do that, then you’ll have to use ajax to make the php call.

couple of tips (if these have to be like the original, then i apologize)

the bit of code where you get the value of GET can be consolicated into 1 line
// Get the search variable from URL
$trimmed = trim(mysql_real_escape_string($_GET[‘term’])) //trim whitespace from the stored variable
the mysql bit in there simply prevents sql injection

Also keep in mind that as you have it now, the php will run when the page is opened, which may be what’s causing the white page. 1 more thing, don’t surpress error messages until the script is working. That’s what the @ does in @$_GET[‘term’];
[php]
if (!isset($var)) {
echo “

We dont seem to have a search parameter!

”;
exit;
}

can be changed to

if (!isset($trimmed)) {
echo “

We dont seem to have a search parameter!

”;
exit;
}

and that can be combined with

if($trimmed == “”) {
echo “

Please enter a search…

”;
exit;
}

to make

if (!isset($trimmed) || empty($trimmed)) {
echo “

We dont seem to have a search parameter!

”;
exit;
}[/php]

Just things to consider. You’ve really got the same things going on in multiple places, but that might changed in future lessons. I just don’t like seeing wasted and confusing lines of code. The php also has to go on top of the page, before the html for it work properly.

First

thank you for time to htlp me up. i’m new to php. most of the code is a copy reference.

So what you mean that my search.html have to convert to search.php and combine both php page into one?
and i have to put the php code function before the html one?

[php]if (!isset($var)) {
echo “

We dont seem to have a search parameter!

”;
exit;
}[/php]

i do not use thi and change to

[php]if (!isset($trimmed) || empty($trimmed)) {
echo “

We dont seem to have a search parameter!

”;
exit;
}[/php]

ok, rename (not convert) search.html to search.php

[php]

<?php //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","root","shaun830511"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("mysql") or die("Unable to select database"); //select which database we're using ?>
New Arrival Car For Sale Prestige No Plate
<?php if(iisset($_POST['Submit'])) { $trimmed = trim(mysql_real_escape_string($_GET['term'])) //trim whitespace from the stored variable $limit = 10; if (!isset($trimmed) || empty($trimmed)) { echo "

We don't seem to have a search parameter!

"; } // Build SQL Query $query = "SELECT * FROM car WHERE class = '$trimmed' ORDER BY type"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "

Results

"; echo "

Sorry, your search: "" . $trimmed . "" returned zero results

"; // google echo "

<a href=\"http://www.google.com/search?q= $trimmed target='_blank' title='Look up $trimmed" on Google">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " LIMIT $s, $limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "

You searched for: "" . $trimmed . ""

"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { echo 'No: '.$row['id']; echo '
type: '.$row['class']; echo '
category: '.$row['cat']; echo '
brand: '.$row['brand']; echo '
year: '.$row['year']; echo '
colour: '.$row['colour']; echo '
mileage: '.$row['mileage']; echo '
note: '.$row['note']; echo '
description: '.$row['descp']; echo '
path: '.$row['name']; echo '






'; echo ""; echo ""; echo '










'; echo "
"; echo "$count.) $title"; $count++; } $currPage = (($s/$limit) + 1); //break before paging echo "
"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " << Prev 10&nbsp "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; }// check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " Next 10 >>"; } $a = $s + ($limit); if ($a > $numrows) { $a = $numrows; } $b = $s + 1; echo "

Showing results $b to $a of $numrows

"; } } ?>[/php]

Aside from some missing html, that shoud work. Its all 1 one page named search.php

hi. thanks… finaly, im able to display search result on the same page.

but there some new question arise.

On my search page, when i open my search page, i hv link some data on the search page as like default data found. when the user search according to the field they want, it will return the search result. until here this is ok with me.

But i found out both the default data and search result data has display on the same page. How am i going to make the default data disappear when i get my search result?

a default search? Add the query st the bottom after the last }.

many thanks to u richei i hv able to display the result result in the same page.

now i hv a new problem.

from the previous, i been doing single image upload accordingly. this is simple and without problem.

now i hv to do a multiple image upload in a single upload session.

what is the best way of it??

in my form

i notice i hv to put it multiple input file

but i cant really upload to my db.

here is my form upload

[code]

Picture Upload 1 Filename: Picture Upload 2 Filename: [/code]

as in my php how can i do a multiple upload ??

this is my current php code
[php]<?php
$connect = mysql_connect(“localhost”, “root”, “shaun830511”) or die (“Error , check your server connection.”);
mysql_select_db(“mysql”);

//Get data in local variable
$v_class=$_POST[‘class’];
$v_cat=$_POST[‘cat’];
$v_brand=$_POST[‘brand’];
$v_model=$_POST[‘model’];
$v_year=$_POST[‘year’];
$v_colour=$_POST[‘colour’];
$v_mileage=$_POST[‘mileage’];
$v_note=$_POST[‘note’];
$v_descp=$_POST[‘descp’];

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{$fileName = $_FILES[‘userfile’][‘name’];
$tmpName = $_FILES[‘userfile’][‘tmp_name’];
$fileSize = $_FILES[‘userfile’][‘size’];
$fileType = $_FILES[‘userfile’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO car (name, size, type, path, class, cat, brand, model, year, colour, mileage, note, descp ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$v_class’,’$v_cat’,’$v_brand’,’$v_model’,’$v_year’,’$v_colour’,’$v_mileage’,’$_note’,’$v_descp’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;
echo “




”;
echo “
Files uploaded
”;

}
?>[/php]

and i hv re create a new table

CREATE TABLE car (
id int(11) not null auto_increment,
class varchar(255) NOT NULL ,
cat varchar(255) NOT NULL,
brand varchar(255),
model varchar(255),
year varchar(255),
colour varchar(255),
mileage varchar(255),
note varchar(255),
descp varchar(2000),
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
path VARCHAR(60) NOT NULL,
timestamp timestamp not null,
name2 VARCHAR(30) NOT NULL,
type2 VARCHAR(30) NOT NULL,
size2 INT NOT NULL,
path2 VARCHAR(60) NOT NULL,
timestamp2 timestamp not null,
name3 VARCHAR(30) NOT NULL,
type3 VARCHAR(30) NOT NULL,
size3 INT NOT NULL,
path3 VARCHAR(60) NOT NULL,
timestamp3 timestamp not null,
name4 VARCHAR(30) NOT NULL,
type4 VARCHAR(30) NOT NULL,
size4 INT NOT NULL,
path4 VARCHAR(60) NOT NULL,
timestamp4 timestamp not null,
name5 VARCHAR(30) NOT NULL,
type5 VARCHAR(30) NOT NULL,
size5 INT NOT NULL,
path5 VARCHAR(60) NOT NULL,
timestamp5 timestamp not null,
name6 VARCHAR(30) NOT NULL,
type6 VARCHAR(30) NOT NULL,
size6 INT NOT NULL,
path6 VARCHAR(60) NOT NULL,
timestamp6 timestamp not null,
PRIMARY KEY (id));

lots of tutorials out there on that. i’m never done it, so i’m not really sure. This one seems pretty decent - http://www.plus2net.com/php_tutorial/php_multi_file_upload.php.

im uploading multi image path into the database… cant really understand what the code really means… kindly hint more about it. tq

hi richei, finally hv tested a correct way to upload my multiple image

however, after i hv done my multiple image upload, my original search result cant be use. this is due to formerly im using single table. now, i nd to search the result in 2 table where there is primary key and foreign key involve.

i hv 2 table

table one consist of information and table two consist of image path where i store it.

there is a primary key and foreign key involve. which table one id is the primary key and table two id is the foreign key references to table one id.

i nd a select statement on how to select a search result base on this 2 table.

All you need between the two tables is a common column, though i don’t know why you need a second table. Just make a column named uploads and use text as the field type. Then store all the file names in that.

hi richei,

i did know how to select two table column in mysql command syntax, but i do not know how to query in php.

the reason im using two table is i was unable to insert all my image path in a single row per entry. my image insert require a few column which include the image name, type, path. i hv tried to create the table which hv 6 column of image name, type and path with different but still not succeed in insert the image path… keep saying my name column cannot hv default value.

below is my current php code

[php]<?php
$connect = mysql_connect(“localhost”, “root”, “shaun830511”) or die (“Error , check your server connection.”);
mysql_select_db(“mysql”);

//Get data in local variable
$v_class=$_POST[‘class’];
$v_cat=$_POST[‘cat’];
$v_brand=$_POST[‘brand’];
$v_model=$_POST[‘model’];
$v_year=$_POST[‘year’];
$v_colour=$_POST[‘colour’];
$v_mileage=$_POST[‘mileage’];
$v_note=$_POST[‘note’];
$v_descp=$_POST[‘descp’];

$query = "INSERT INTO car (class, cat, brand, model, year, colour, mileage, note, descp ) ".
“VALUES (’$v_class’,’$v_cat’,’$v_brand’,’$v_model’,’$v_year’,’$v_colour’,’$v_mileage’,’$_note’,’$v_descp’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile’][‘name’];
$tmpName = $_FILES[‘userfile’][‘tmp_name’];
$fileSize = $_FILES[‘userfile’][‘size’];
$fileType = $_FILES[‘userfile’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile2’][‘name’];
$tmpName = $_FILES[‘userfile2’][‘tmp_name’];
$fileSize = $_FILES[‘userfile2’][‘size’];
$fileType = $_FILES[‘userfile2’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile3’][‘name’];
$tmpName = $_FILES[‘userfile3’][‘tmp_name’];
$fileSize = $_FILES[‘userfile3’][‘size’];
$fileType = $_FILES[‘userfile3’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile4’][‘name’];
$tmpName = $_FILES[‘userfile4’][‘tmp_name’];
$fileSize = $_FILES[‘userfile4’][‘size’];
$fileType = $_FILES[‘userfile4’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile5’][‘name’];
$tmpName = $_FILES[‘userfile5’][‘tmp_name’];
$fileSize = $_FILES[‘userfile5’][‘size’];
$fileType = $_FILES[‘userfile5’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

$uploadDir = ‘images/’;

if(isset($_POST[‘upload’]))
{
$fileName = $_FILES[‘userfile6’][‘name’];
$tmpName = $_FILES[‘userfile6’][‘tmp_name’];
$fileSize = $_FILES[‘userfile6’][‘size’];
$fileType = $_FILES[‘userfile6’][‘type’];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}

include ‘…/library/config.php’;
include ‘…/library/opendb.php’;

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$picid=$_POST[‘picid’];

$query = "INSERT INTO carimage (name, size, type, path, picid ) ".
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$filePath’, ‘$picid’)”;

mysql_query($query) or die('Error, query failed : ’ . mysql_error());

include ‘…/library/closedb.php’;

echo “
Files uploaded
”;

}

?>[/php]

i do know i hv some very stupid code that i hv did many changes in order to get the multiple image upload working.

please give me some help which im abit lost in it…

Sponsor our Newsletter | Privacy Policy | Terms of Service