Jump to content

SELECT WHERE AND


rvdb86

Recommended Posts

Hi, really hope someone can help me out...

 

I have a form with two dropdown lists as follows:

 

<form name="quick_find" action="search_result.php" method="get">
      <select name="var1">
        <option value="op1">1</option>
        <option value="op2">2</option>
        <option value="op3">3</option>
      </select>
       <select name="var2">
        <option value="op4">1</option>
        <option value="op5">2</option>
        <option value="op6">3</option>
      </select>

 

I have a MySQL table with 3 columns.

 

Basically i want to select records from the table using the two variables so i tried the following:

 

$query = "SELECT * FROM table WHERE column1 = $var1 AND column2 = $var2";
$result = mysql_query($query)
or die ("Couldn't execute query");

 

This returns the result: "Couldn't execute query"  :(

I used the $_GET function to ensure that the variables exsist but with no luck.

 

I would really appreciate any suggestions!

Link to comment
Share on other sites

Just use POST and then (or you could use GET if you want, but Post wont show the variables).

 

$var1 = $_POST['var1'];
$var2 = $_POST['var2'];

$query = "SELECT * FROM table WHERE column1 = '$var1' AND column2 = '$var2'";
$result = mysql_query($query)
or die ("Couldn't execute query");
[/code

Link to comment
Share on other sites

Try enclosing the variables with single quotes.

$query = "SELECT * FROM table WHERE column1 = '$var1' AND column2 = '$var2'";
$result = mysql_query($query)
or die ("Couldn't execute query");

 

Edit:

but Post wont show the variables

Yes it will, it's just as easy to see the post data as it is to see the get data.

 

Link to comment
Share on other sites

Thanks alot for your suggestions but i am still having problems. I have posted below my code with an explanation in hope that someone could can help me out...

 

I have 2 tables:

1. products_to_categories

2. products_to_products_extra_fields

 

I want first of all to select all products that mach a category from the products_to_categories table so i used the first query:

<?php


$query = "SELECT * FROM products_to_categories WHERE categories_id = '$cut'";
$result = mysql_query($query)
or die ("Couldn't execute query");


$counter = 1;
while ($row = mysql_fetch_array($result))
{
extract($row);

 

Then for each product that maches the category i want to show a the results from the products_to_products_extra_fields table, so within the while loop (so i get each product_id from the products_to_categories table) i have the following query:

 


$query2 = "SELECT * FROM products_to_products_extra_fields WHERE products_id = '$products_id' AND $products_extra_fields_value = '$colour'";
$result2 = mysql_query($query2)
or die ("Couldn't find colour");


while ($row2 = mysql_fetch_array($result2))
{
extract($row2);

echo"

  <tr>
    <td>$products_id</td>
    <td>$categories_id</td>
    <td>$products_extra_fields_id</td>
    <td>$products_extra_fields_value</td>
  </tr>

";
}

}
?>

 

This produces: "Couldn't find colour"

 

I would really appreciate it if someone could tell me where im going wrong

Link to comment
Share on other sites

Here is what i got:

 

Couldn't find colour, ERROR: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 '= 'D'' at line 1

 

I should mention that "D" is the value of $colour.

 

Thanks alot for taking the time to try help.

Link to comment
Share on other sites

The variable comes from a form on a different page. I need the user to select the category they want and the colour, which they can choose from a dropdown list. Eventually once i get this working there will be 2 more options for the user. I have included the code of my form if it helps.

 

<form action="find_results.php" method="post" name="find">            
      <select style="width:200px;" name="category">
        <option value="30">Round</option>
        <option value="31">Princess</option>
        <option value="32">Emerald</option>
        <option value="33">Alternative</option>
      </select>
<!-- ### EOF SELECT CUT ### -->
  <tr style="background-color:#eff8fd;">
    <td>Colour:</td>
    <td>
<!-- ### SELECT COLOUR ### --> 
  <select style="width:200px;" name="colour">
    <option value="D">D - Best</option>
    <option value="E">E - Exceptional White</option>
    <option value="F">F - Very Very White</option>
    <option value="G">G - Very White</option>
    <option value="H">H - White</option>
    <option value="I">I - Some Colour</option>
  </select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.