Jump to content

[SOLVED] Do I have syntax errors of some kind in the Select statement?


Recommended Posts

On the search form the user inputs the word "power" to search for records in the db.

 

In addition, on the search form, the user selects a field to search by selecting a radio button. The selected radio button's value is passed to the php script. The radio button values are converted to the values of Topic, Subtopic, or Theswords in the following block of code.

 

$RBFieldSelect = $_GET['RB1'];
if(!empty($RB1)){
if ($RBFieldSelect == "V1")
  $fieldName1 = 'Topic';  // AND $RBFielsSelect = "''";
   }
   else
   {
   $RBFieldSelect = "''";
   }

$RBFieldSelect = $_GET['RB1'];
if(!empty($RB1)){
if ($RBFieldSelect == "V2")
$fieldName2 = 'Subtopic'; // AND $RBFielsSelect = "''";
   }
   else
   {
   $RBFieldSelect = "''";
   }
   
   
$RBFieldSelect = $_GET['RB1'];
if(!empty($RB1)){
if ($RBFieldSelect == "V3")
$fieldName3 = 'Theswords'; // AND $RBFielsSelect = "''";
   }
   else
   {
   $RBFieldSelect = "''";
   }

 

 

If the user selects the Topic selection from the radio buttons, when the script is run, here is the value echo'd out.

 

fieldName1....=....Topic

fieldName2....=....

fieldName3....=....

 

The query statement picking up the values above is as follows.

 

$query2 = "SELECT * FROM `View2_Concord` WHERE $fieldName1 LIKE '%$SeeAlso%' OR $fieldName2` LIKE '%$SeeAlso%' OR $fieldName3 LIKE '%$SeeAlso%' AND `Source` IN ($NV,  $TR, $BT) ORDER BY `Lnum` ASC LIMIT $startrecord, $display";

 

The error message from php is below.

 

Unknown column ' LIKE '%power%' OR ' in 'where clause'

 

Do I have syntax errors of some kind in the above Select statement? Thank you in advance for your help.

 

 

Do I have syntax errors of some kind in the above Select statement?

 

Yes. Just remove all those backticks. There is no need for them, and you are missing one.

 

$query2 = "SELECT * FROM View2_Concord WHERE $fieldName1 LIKE '%$SeeAlso%' OR $fieldName2 LIKE '%$SeeAlso%' OR $fieldName3 LIKE '%$SeeAlso%' AND Source IN($NV,  $TR, $BT) ORDER BY Lnum ASC LIMIT $startrecord, $display";

Thank you thorpe for the comment.

 

I changed the query to the following per your suggestions.

 

$query = "SELECT * FROM View2_Concord WHERE $fieldName1 LIKE '%$SeeAlso%' OR $fieldName2 LIKE '%$SeeAlso%' OR $fieldName3 LIKE '%$SeeAlso%' AND Source IN ($NV,  $TR, $BT) ORDER BY Lnum ASC";

 

Now I am getting a new error message seen below.

 

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 'LIKE '%power%' OR LIKE '%power%' AND Source IN ('NV', 'TR', 'BT') ORDER BY L' at line 1

 

Because the user selected only one field to search, the variables $fieldName2 and $fieldName3 are empty when the query is run.

 

Do you have any suggestions as to how I can get rid of the error message and get the script to run as it should?

Im just going to post an example. You need to build your query dynamically.

 

<?php

  $sql = "SELECT * FROM foo WHERE fld = 'bar'";

  if (isset($var1)) {
    $sql .= " AND fld2 = 'boo'";
  }

  if (isset($var2)) {
    $sql .= " AND fld3 = 'bob'";
  }

?>

Since I do not know enough to code the above solution by Thorpe, here is a less sophisticated solution. 

 

The input box variable coming into the php script is $SeeAlso. What the following code does is test whether the user has selected a field to search. If the user selects a field to search, a "substitute" $SeeAlso variable is assigned $SeeAlso. If a fieldname is not selected by the user, then the $SeeAlso variable is assigned a value that does not exist in the database, i.e. EditWaldron1, EditWaldron2, or EditWaldron3. This code has disadvantages in that searches are made for non-existant values.

 

if(!empty($fieldName1)){
  $SeeAlso1 = $SeeAlso;
  $fieldName1 = 'Topic';
   }
   else
   {
   $SeeAlso1 = 'EditWaldron1';
   $fieldName1 = 'Topic';
   }
  if(!empty($fieldName2)){
   $SeeAlso2 = $SeeAlso;
   $fieldName2 = 'Subtopic';
   }
   else
   {
   $SeeAlso2 = 'EditWaldron2';
   $fieldName2 = 'Subtopic';
   }
  if(!empty($fieldName3)){
    $SeeAlso3 = $SeeAlso;
    $fieldName3 = 'Theswords';
   }
   else
   {
    $SeeAlso3 = 'EditWaldron3';
    $fieldName3 = 'Theswords';
   }

$query = "SELECT * FROM View2_Concord WHERE $fieldName1 LIKE '%$SeeAlso1%' OR $fieldName2 LIKE '%$SeeAlso2%' OR $fieldName3 LIKE '%$SeeAlso3%' AND Source IN ($NV,  $TR, $BT) ORDER BY Lnum ASC";

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.