Trouble putting $_POST value into a MySQL Query

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
TimFromPhx
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 6:14 pm

Trouble putting $_POST value into a MySQL Query

Post by TimFromPhx »

I am new to PHP but not to coding and I am at a loss. I can't get this query to work properly. The general purpose of the code is selecting a record from a MySQL table into a combobox on the first form and using that record to get it's relational children put into another combobox on the called form. I am trying to put the $_POST value into the Query but it does not return any records. It should return 1 exact match. I have gotten the $_POST value from a combobox on the previous form which called this code. When I echo the $baseProduct it shows up correctly so the $_POST value is there. I am not looping through the array because I should only have one record. I think it is a syntax problem in the query. Any ideas would be appreciated.

Code: Select all

 
      <?php $Selected_DB = mysql_select_db ("highdollaritems");?> 
      
      <?php 
                $baseProduct = $_POST["selectedProduct"];
                
                echo $baseProduct;// Test to see if value is there
                
                $baseProductQuery = "SELECT * FROM BaseProducts WHERE BaseProducts.SWProductName = '$baseProduct'";
                                
                $result1 = mysql_query($baseProductQuery);
                $row1 = mysql_fetch_assoc($result1);
                $baseProductUId = $row1['UniqueID'];
                echo $baseProductUId;  // Test to see if value is there
      ?>
      <?php 
            
                                
              $query = "SELECT * FROM `Accessories` WHERE Accessories.UniqueID = '$baseProductUId'";
            
              $result2 = mysql_query($query);
        while ($row = mysql_fetch_array($result2)) {
                    $UniqueID[]             = $row['UniqueID'];
                    $SWAccessoryID[]        = $row['SWAccessoryID'];
                    $SWAccessoryName[]      = $row['SWAccessoryName'];
                    $AccessoryDescription[] = $row['AccessoryDescription'];
                    $Price[]                = $row['Price'];
          }
      ?>
      
      <SELECT name = "SWAccessoryName" size="1" id="Combobox1">
        <?php
                    $option = "<option VALUE=\"Select a Pool Package\">Please select a Pool Package</option> \n";
                        for ($i = 0; $i < COUNT($SWAccessoryName); $i++) {
                            $option .= "<option ";
                            $option .= "value=\"$SWAccessoryName[$i]&nbsp;&nbsp;&nbsp;$$Price[$i]\">$SWAccessoryName[$i]&nbsp;&nbsp;&nbsp;$$Price[$i]</option> \n";
                            }
                    echo $option;
        ?>
      </SELECT>
      <INPUT name="SubmitForm" type="submit" class="orderInputFields" value="Create Sales Order"></TD>
 
 
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Trouble putting $_POST value into a MySQL Query

Post by susrisha »

hello...
can you try to echo the sql statement and run the same on mysql and see if it produces any results?

Code: Select all

 
echo $baseProductQuery;
 
 
also try to echo the number of rows that the query returned

Code: Select all

 
echo mysql_num_rows($result1);
//try an if statement like this
if(!$result1 = mysql_query($baseProductQuery))
{
echo mysql_error();
}
 
TimFromPhx
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 6:14 pm

Re: Trouble putting $_POST value into a MySQL Query

Post by TimFromPhx »

I get a no response to the row count query.
I received the following SQL 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"

When I run this query manually on the MySQL Server it works perfectly. I even took the PHP code that was created by the server and used it and it failed.
TimFromPhx
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 6:14 pm

Re: Trouble putting $_POST value into a MySQL Query

Post by TimFromPhx »

I rebuilt the query in the following way because the error was occuring because there were ' characters in the data and it works. There must be some way to format a query so ` ' " characters in the data don't screw up the results returned.

Any ideas?

Code: Select all

 
 
                $baseProduct = $_POST["selectedProduct"];
                $selClause = "SELECT * FROM BaseProducts ";
                $whereClause = "WHERE BaseProducts.SWProductName = ";
                $baseProductQuery = $selClause.$whereClause.'"'.$baseProduct.'"';
 
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Trouble putting $_POST value into a MySQL Query

Post by tech603 »

I think this is what you are looking for

http://us.php.net/mysql_real_escape_string

This will format the string before sending into your mysql database.

Hope this helps.

Matthew Vass
QA Analyst
mvass@hostmysite.com
http://www.hostmysite.com?utm_source=bb
Post Reply