Hello everyone, this is my first post here so I will try to make it as clear as possible.
Firstly i have a database with a single table named "albums" with the following fields: -
albumid int(11)
-
genreid varchar(30)
-
albumtitle varchar(30)
-
albumauthor varchar(30
-
numberofsongs int(11
-
price int(11)
Then i have two php pages
Search.php : -
<html>
-
-
<center>
-
-
</br></br></br></br>
-
-
<img src="guitarlogo.jpg" alt="Music Database"/>
-
-
<form method="GET" action="Result.php">
-
-
<h2><b>Search Music Database:</b></h2>
-
-
<input type="text" name="mts" />
-
<input type="submit" name="submit" value="Search" />
-
</form>
-
-
<?php
-
$connection = mysql_connect("localhost","root","");
-
$fields = mysql_list_fields("ea09039", "albums", $connection);
-
$columns = mysql_num_fields($fields);
-
echo "<form action=Result.php method=POST>";
-
-
echo "<select name=Field>";
-
for ($i = 0; $i < $columns; $i++) {
-
echo "<option value=$i>";
-
echo mysql_field_name($fields, $i);
-
}
-
echo "</select></form>";
-
?>
-
-
</center>
-
-
</html>
-
and Result.php : -
<?php
-
-
include("db.php");
-
-
$mts=$_GET["mts"];
-
$Field=$_GET["Field"];
-
-
-
$query="select * from albums where '$Field' like '%$mts%'";
-
-
$res2=mysql_query($query);
-
-
while($row=mysql_fetch_row($res2))
-
{
-
echo $row[2];
-
echo " has author ";
-
echo $row[3];
-
echo " and costs ";
-
echo $row[5];
-
echo " Euro.";
-
echo " And is from the genre ";
-
echo $row[1];
-
echo "<br/>";
-
}
-
-
?>
-
This is as far as i have gotten with this, but i keep getting errors.
Please if someone can help I would be very thankful.
12 7104 JKing 1,206
Recognized Expert Top Contributor
Okay, so what are your errors? What is it doing? What is it supposed to be doing?
Right now it's just giving me a blank page.
I wrote the word "blue" in the textbox and selected "albumtitle " from the dropdown list(which is dynamically popullated from a table in a MySQL database).
This is what i get at the address bar after i do a search: - http://localhost/ea09039/HOMEWORK2/Result.php?mts=blue&submit=Search
As for what is it supposed to be doing,
I need it to take the input from the textbox, and search within the column from the table that is selected from the dropdown list which are in Search.php and show the results in Result.php.
I hope i was clear :)
JKing 1,206
Recognized Expert Top Contributor
It looks like you have two forms on your search.php page. The first form only has a text input for mts and a submit button. These are sent to result.php via GET.
Your second form has a select named field and this form is sent to result.php via POST however there is no submit button for this form.
On result.php you try to retrieve the field variable via $_GET but you have never sent it with GET or at all because it is part of another form that doesn't get submitted and if it were sent it would be in the $_POST.
Now since your $field variable is never populated your query fails and so does your while loop and you end up with no output.
The solution here is to make the select part of your first form.
Also use mysql_real_esca pe_string() on any variables you pass to the database.
Okay thank you for the reply.
I restructured my code and I believe there is one more problem left.
Here is the code for Search.php -
<html>
-
-
<center>
-
-
</br></br></br></br>
-
-
<img src="guitarlogo.jpg" alt="Music Database"/>
-
-
<h2><b>Search Music Database:</b></h2>
-
<?php
-
$connection = mysql_connect("localhost","root","");
-
$fields = mysql_list_fields("ea09039", "albums", $connection);
-
$columns = mysql_num_fields($fields);
-
-
echo "<form action=Result.php method=GET>";
-
echo "<input type=text name=mts/>";
-
echo "<select name=Field>";
-
-
for ($i = 0; $i < $columns; $i++) {
-
echo "<option value=$i>";
-
echo mysql_field_name($fields, $i);
-
}
-
echo "</select>";
-
echo "<input type=submit name=submit value=Search />";
-
?>
-
-
-
</center>
-
-
</html>
-
The problem is that the Field variable returns the number of the position on the table. For example If i select albumtitle from the dropdown the Field variable return the position of the column albumtitle which is 2. I need it to return the name of that column not it's position.
Here is the result of the address barr - http://localhost/ea09039/HOMEWORK2/Result.php?mts/=blue&Field=2&submit=Search
Here is the Results.php file again: -
<?php
-
-
include("db.php");
-
-
$mts=$_GET["mts"];
-
$Field=$_GET["Field"];
-
-
- $query="select * from albums where '$Field' like '%$mts%'";
-
-
$res2=mysql_query($query);
-
-
while($row=mysql_fetch_row($res2))
-
{
-
echo $row[2];
-
echo " ka autor ";
-
echo $row[3];
-
echo " dhe kushton ";
-
echo $row[5];
-
echo " Euro.";
-
echo " Dhe eshte nga zhanri ";
-
echo $row[1];
-
echo "<br/>";
-
}
-
-
?>
-
Any ideas?
Not what I'm looking for, but it might come in handy in the future. Thanks anyway
JKing 1,206
Recognized Expert Top Contributor
The problem is that the Field variable returns the number of the position on the table. For example If i select albumtitle from the dropdown the Field variable return the position of the column albumtitle which is 2. I need it to return the name of that column not it's position.
Well of course it is. You are setting all your option values equal to $i in your for loop and not the field name.
Okay thank you i understand my mistake, but could you please show me how should the code look, i can not seem to figure it out.
P.S im new to php so please have patience with me :)
- $_id = $_POST['Field'];
-
-
while($row=mysql_fetch_row($res2)) {
-
if ( $row['id'] == $_id ) {
-
$_name = $row['name'];
-
}
-
}
Or something like that. Basicly compare the id from the $_POST variable or instead of that... - $connection = mysql_connect("localhost","root","");
-
$fields = mysql_list_fields("ea09039", "albums", $connection);
-
$columns = mysql_num_fields($fields);
-
-
echo "<form action=Result.php method=GET>";
-
echo "<input type=text name=mts/>";
-
echo "<select name=Field>";
-
-
for ($i = 0; $i < $columns; $i++) {
- echo "<option value='". mysql_field_name($fields, $i). "'>";
-
echo mysql_field_name($fields, $i);
-
}
-
echo "</select>";
-
echo "<input type=submit name=submit value=Search />";
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: vichet |
last post by:
Hi All;
Please help me with some problem
i want VBSCRIPT to search something in only my own website
give me code
thank
vichet
|
by: sathyashrayan |
last post by:
dear group,
I have been working VC++ for some time. My company assigned me a task for
an online dictionary search
site similar to the onelook.com which I have to make it in php mysql. Since
I never did any of the project in the search engine , can any one guide me
to the above?
I am really struggling to understand the following:
When I...
|
by: Martien van Wanrooij |
last post by:
I have been using phpdig in some websites but now I stored a lot of larger
texts into a mysql database. In the phpdig search engine, when you entered a
search word, the page where the search word was found was displayed with
about 2 lines before and 2 lines behind the search word itself. Let us say
you look for "peanut butter" an the word is...
|
by: hazly |
last post by:
I'm very new in the web technology and need advice on search engine. I
want to develop a portal using PHP and MySQL on Linux. Need to know on
the following features :
1. search engine that could search my portal (mySQL, PDF, Ms Word &
others)
2. search engine that could search to few web sites specified by
user/programmer
|
by: JJ_377 |
last post by:
I am trying to populate a two-column dropdown list from a populated
datareader (dr) having two fields from the Categories table of the
Northwind database: CategoryID and CategoryName.
I've used this type of construction successfully with VB.NET (from
which I am now transistioning...) and it has worked unfailingly with my
dropdowns there.
...
| |
by: gaganlatha |
last post by:
Hi,
I want to create a search engine for my site. When i search a any keyword it should take me to that keyword. The keyword may be a country name, name, month etc. If this available in database it should take me there else if the keyword specified by the user is new it should store that keyword in database and when next time the user give that...
|
by: d1156676 |
last post by:
Hi
I have a Dynamic dropdown reading in data from a MySql database, I need to refresh the dropdown when the database is updated without refreshing the whole page.
I have had a go at trying to use a javascript and getelementbyid but with no sucess.
Does any ones have any example or have any ideas on how i can do this.
Any help will...
|
by: miraan |
last post by:
Hi,
I have searched on the internet but can't find a way to disguise a long ugly php url such as www.domain.com/companies.php?id=543 into this:
www.domain.com/companyname
the folder "companyname" shouldnt really exist. There should be some instructions in .htaccess or something like that (not sure) to show www.domain.com/companies.php?id=543...
|
by: nagendra802000 |
last post by:
Hi All,
I have created a search engine for MP3 with mysql database. Now I want
to connect each search result with its respective info page which will
consist with name of the artist, size, year, and so on. Dose anyone
knows how to do it? I did created few static pages with all these
info. But its impossible to create for thousands of MP3s....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |