Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dropdown box query sql database HELP!

    ello there!

    I'm a noob at this you'll no doubt notice

    My problem is, i want to create a feature similar to this one: http://www.jewson.co.uk/en/templates...uestid=2019469

    As youll see, you can sort the database by manufacturer, color etc in a way that you can select colour from a particular manufcaturer too.

    They use java servlets, i want to use php + mysql.

    I have a database in place... i have built a search.htm form with four drop down menus.

    This is the results.php page which i know is wrong and doesnt work.

    Code:
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$dbName") or die( "Unable to select database");
    
    function secured($val)
    {
    $val = strip_tags(trim(($val))) ;
    $val = escapeshellcmd($val);
    return stripslashes($val);
    }
    if (get_magic_quotes_gpc())
    {
    $Manufacturer = $_POST['Manufacturer'];
    }
    else
    {
    $Manufacturer = addslashes($_POST['Manufacturer']);
    }
    $Manufacturer = secured($Manufacturer);
    
    if ($Manufacturer!= "SelectManufacturer") //The Default select value
    {
    $query = "SELECT * FROM bricks WHERE Manufacturer=$Manufacturer1";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    if(!$num_rows)
    {
    echo "<table><tr><td align='center'>No data found 0 record returned</td></tr></table>";
    }
    else
    {
    echo "<table align='center'><tr>";
    echo "<td align='center'><b>Manufacturer</b></td>";
    echo "<td align='center'><b>Colour</b></td>";
    echo "<td align='center'><b>Texture</b></td>";
    echo "<td align='center'><b>Manufacturing Process</b></td>";
    echo "</tr>";
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
    echo "<td align='center'>".$row["Colour"]."</td>";
    echo "<td align='center'>".$row["Texture"]."</td>";
    echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
    echo "</tr>";
    
    }
    echo "</table>";
    }
    ?>
    brings up :

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/thetdco/public_html/brick/results.php on line 31
    No data found 0 record returned

    HElp!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You don't have a variable called Manufacturer1

    PHP Code:
    $query "SELECT * FROM bricks WHERE Manufacturer=$Manufacturer1"
    Take the 1 off it

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You should setup error checking on your query's too. And echo at your query when you get a failed recordset to make sure you are indeed getting the values necessary for you query as Nightfire suggested.

    $result = mysql_query($query) or die("failed: mysql_error());

  • #4
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://www.thetechworld.co.uk/brick/search.htm

    Thats my html form

    I've tried error reporting in multiple places "OR DIE..."

    It just brings up more errosr like unexpected ">" in line 34

    Why is it saying it isnt valid resutl resource?

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    $query "SELECT * FROM bricks WHERE Manufacturer=$Manufacturer1";
    //take off the '1' on Manufacturer as suggested my Nightfire
    echo $query;  //make sure you getting the right values
    $result mysql_query($query) or die ("failed:".mysql_error());
    $num_rows mysql_num_rows($result); 
    Try that see what you get.

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    That's as he's forgotten to use closing quotes
    Your drop down for manufacturers you've called "Select Manufacturer" you can't have a name with a gap in. Take out Select
    PHP Code:
    $result mysql_query($query) or die("failed: ".mysql_error()); 

  • #7
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    SELECT * FROM bricks WHERE Manufacturer=failed: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 '' at line 1

    Thats what happens when i replaced original code with the one suggested

    line 1 being :

    <?
    $hostname = "localhost";
    $username = ""; .
    $password = "";
    $usertable = "bricks";
    $dbName = "tt";

    removed the details

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You need to wrap single quotes around your value

    $query = "SELECT * FROM bricks WHERE Manufacturer='$Manufacturer'

    And by putting the die message there, it told you that. You will learn what different error's tell you

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You've still got the drop down called "Select Manufacturer". You can't give it a name with a space. As I said before, delete Select. Rename $Manufacturer1 to $Manufacturer in your query

    <select size="1" name="Select Manufacturer">

  • #10
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now the page displays

    SELECT * FROM bricks WHERE Manufacturer=''
    No data found 0 record returned

    Thanks guys for all your help btw. i appreciate it

  • #11
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Your not getting your $Manufacturer value obviously, since its blank... Fix that and you should be golden.

  • #12
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by Nightfire View Post
    You've still got the drop down called "Select Manufacturer". You can't give it a name with a space. As I said before, delete Select.

    <select size="1" name="Select Manufacturer">
    3rd time lucky.....

  • #13
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have edited the space out of the SelectManufacture... still no luck.

    Im wondering where is the word "Manufactuerer" defined in my search html so that the php can POST it

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 1</title>
    </head>
    
    <body>
    
    <form method="POST" action="http://www.thetechworld.co.uk/brick/results.php" target="_blank">
    	<p><select size="1" name="SelectManufacturer">
    	<option value="Manufacturer1">Manufacturer1</option>
    	<option value="Manufacturer2">Manufacturer2</option>
    	<option value="Manufacturer3">Manufacturer3</option>
    	<option selected value="SelectManufacturer">SelectManufacturer</option>
    	</select></p>
    	<p><select size="1" name="Brown">
    	<option selected value="Select Colour">Select Colour</option>
    	<option value="Red">Red</option>
    	<option value="Blue">Blue</option>
    	<option value="Brown">Brown</option>
    	</select></p>
    	<p><select size="1" name="rough">
    	<option selected>Select Texture</option>
    	<option value="smooth">smooth</option>
    	<option value="rough">rough</option>
    	</select></p>
    	<p><select size="1" name="Pressed">
    	<option selected value="SelectManufacturingProcess">SelectManufacturingProcess
    	</option>
    	<option value="Wire Cut">Wire Cut</option>
    	<option value="Hand Made">Hand Made</option>
    	<option value="Stock">Stock</option>
    	<option value="Pressed">Pressed</option>
    	</select></p>
    	<p><input type="submit" value="Submit" name="B1"></p>
    	</form>
    <p>&nbsp;</p>
    </body>
    
    </html>

  • #14
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Delete the word Select (sigh lol)

    Or rename the variable
    PHP Code:
    $Manufacturer $_POST['Manufacturer']; 
    to
    PHP Code:
    $Manufacturer $_POST['SelectManufacturer']; 

  • #15
    New Coder
    Join Date
    Apr 2007
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok that made it better, now its displaying the table for the records and it is displaying a record. Im not sure why the SELECT * FROM query is still showing thoguh?

    SELECT * FROM bricks WHERE Manufacturer='Manufacturer1'
    Manufacturer Colour Texture Manufacturing Process
    Manufacturer1


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •