HTML dropdown, PHP Variables.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nick.phillips
Forum Newbie
Posts: 3
Joined: Fri Sep 14, 2007 6:09 am

HTML dropdown, PHP Variables.

Post by nick.phillips »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey all,
  Firstly as probably all posts start with, I'm new to PHP. I was thrown into it by my Boss. I've managed to query a very basic search, delete, edit and add to interact with a SQL database, however, hes now asking for dropdown menus and such. I know how to create one in HTML, and how to assign variables, post them to the next page. Im just a bit stuck where to go next. I have a piece of code i drew up, but its just not working.

I'm pretty sure that its the Query thats wrong, or that im assigning the variables wrongly.
Normally im against posting whole chunks of code, but im really stuck.

So Page 1
[syntax="html"]<FORM ACTION="searchresults01.php" METHOD="post">
  <p>Search for:
    <INPUT NAME="search" TYPE="text"> 
    in table:
    <select name="dropdown" id="dropdown">
      <option value="ALLTABLES" selected="selected">*</option>
      <option value="BOOKTABLE">BOOKS</option>
      <option value="FURNTABLE">FURNITURE</option>
      <option value="ITTABLE">IT</option>
      <option value="KITCHENTABLE">KITCHEN</option>
      <option value="MISCTABLE">MISC</option>
      <option value="ARCHIVETABLE">ARCHIVE</option>
        </select>
    <INPUT TYPE="submit" VALUE="Proceed" name = "dropdown">
</p>
  <p>
    <label></label>
  </p>
</FORM>
Page 2[/syntax]

Code: Select all

<?
$search = $_POST['search'];
$dropdown = $_POST['dropdown'];
$ALLTABLES = $_POST['ALLTABLES'];
$BOOKTABLE = $_POST['BOOKTABLE'];
$FURNTABLE = $_POST['FURNTABLE'];
$ITTABLE = $_POST['ITTABLE'];
$KITCHENTABLE = $_POST['KITCHENTABLE'];
$MISCTABLE = $_POST['MISCTABLE'];
$ARCHIVETABLE = $_POST['ARCHIVETABLE'];
	
	$db = mysql_connect("localhost","rarar","blabla");
	mysql_select_db("asset tracker", $db);
	$query = "SELECT * FROM '%".$ALLTABLES."%' || '%".$BOOKTABLE."%' || '%".$FURNTABLE."%' || '%".$ITTABLE."%' || '%".$KITCHENTABLE."%' || '%".$MISCTABLE."%' || '%".$ARCHIVETABLE."%' WHERE item || loan || company LIKE '%".$search."%'";
	$result = mysql_query($query)or die(Error, query failed.);

	while ($item = mysql_fetch_assoc($result)) {
	while (list($fieldname, $fieldvalue) = each ($item)) {
	
		echo $fieldname.": <b>".$fieldvalue."</B><br>";
	
	} echo "<p>";
	}
	?>
What this should do, is allow the user to search for "x" item in the selected table. Which is then echoed out.
Is there something that i am missing, or a syntax error?
I'm going bald!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

You have assigned the same name twice inside your form - the select element and the submit button. Give them different names.

In page 2 you should ideally check whether the form has been submitted

Code: Select all

if(isset($_POST['nameOfButton']))
In your query replace the || with OR.

Also remove the " double quotes and . periods when specifying the variables within the query - it should just look like

Code: Select all

'%$var%'
In your die() params replace your custom error with mysql_error() e.g. die(mysql_error()) (this will give you a more specific error).
nick.phillips
Forum Newbie
Posts: 3
Joined: Fri Sep 14, 2007 6:09 am

Post by nick.phillips »

Thanks for the quick response, Ace.
I've amended what you've suggested but the damn thing still isn't working.
Although i'm now getting something along the lines of this error being displayed;

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''%BOOKTABLE%' WHERE item OR loan OR company LIKE '%css book%'' at line 1
Of course there isn't anything of interest on line one apart from the HTML start.
I think the problem now is that the table names arn't being picked up from the drop down box.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Could you post your new code please?
nick.phillips
Forum Newbie
Posts: 3
Joined: Fri Sep 14, 2007 6:09 am

Post by nick.phillips »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Certainly, Sorry!

Code: Select all

<?
$search = $_POST['search'];
$dropdown = $_POST['dropdown'];
$ALLTABLES = $_POST['ALLTABLES'];
$BOOKTABLE = $_POST['BOOKTABLE'];
$FURNTABLE = $_POST['FURNTABLE'];
$ITTABLE = $_POST['ITTABLE'];
$KITCHENTABLE = $_POST['KITCHENTABLE'];
$MISCTABLE = $_POST['MISCTABLE'];
$ARCHIVETABLE = $_POST['ARCHIVETABLE'];

	$db = mysql_connect("localhost","blabla","blablablaa");
	mysql_select_db("asset tracker", $db);
	$query = "SELECT * FROM '%$dropdown%' WHERE item OR loan OR company LIKE '%$search%'";
	$result = mysql_query($query)or die(mysql_error());

	while ($item = mysql_fetch_assoc($result)) {
	while (list($fieldname, $fieldvalue) = each ($item)) {
	
		echo $fieldname.": <b>".$fieldvalue."</B><br>";
	
	} echo "<p>";
	}
	?>
Like i said, think its because the Variable to each of the Menu's items, arnt linked to the database. Im not to sure how to do that, or even if thats right.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by nick.phillips on Mon Sep 17, 2007 2:52 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

1. Make sure you have named your select element and submit button differently

2. Remove both % from '%$dropdown%'. It is just the variable value you want. % signs are used for LIKE queries.

3. Change your WHERE clause so that each clause states a condition i.e.

Code: Select all

item='$something' OR loan LIKE '%$something%'
You cant have a continuous line of OR statements without stating a condition for each one.

Hope this makes sense.
Post Reply