1

This code down here should search database. but I am getting error that my table doesnt exists. And also I want to ask why if I push second time submit button it just jumps to else so it echo choose at least.... and also all data from database. Thanks!

Here is php

    if (isset($_POST['submit'])) {                                                   
   $query = 'SELECT * FROM station_tab';
   if (!empty($_POST['station_name']) && !empty($_POST['city']) && !empty($_POST['zone'])) 
   {
      $query .= 'WHERE station_name' .mysql_real_escape_string($_POST['station_name']) . 'AND city' . mysql_real_escape_string($_POST['city']) . 'AND zone' . mysql_real_escape_string($_POST['zone']);   
   } elseif (!empty($_POST['station_name'])) {
      $query .= 'WHERE station_name' . mysql_real_escape_string($_POST['station_name']);
   } elseif (!empty($_POST['city'])) {
      $query .= 'WHERE city' . mysql_real_escape_string($_POST['city']);
   } elseif (!empty($_POST['zone'])) {
      $query .= 'WHERE zone' . mysql_real_escape_string($_POST['zone']);
   } else {
   echo "Choose at least one option for search";
   }
    $result = mysql_query($query, $db) or die(mysql_error($db));    
    if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($result)){    
    echo '<br/><em>' .$row['station_name'] . '</em>';
    echo '<br/>city: '. $row['city'];  
    echo '<br/> zone: ' .$row['zone'];
    echo '<br/> Long: ' .$row['lon'];
    echo '<br/> Lat: ' . $row['lat'];  
    }    
  }
  } 

here is error message when I add name of the city to city.

Table 'stanice_tab.station_tabwhere' doesn't exist
6
  • Are you sure you're connecting to the right database? Commented Sep 26, 2015 at 20:25
  • yes I am, because it echos all data from station_tab :) Commented Sep 26, 2015 at 20:27
  • So when are you getting the "table does not exists" error? Commented Sep 26, 2015 at 20:28
  • I have edited question and added error. Commented Sep 26, 2015 at 20:30
  • 1
    you must have a equal sign between the field and the value it the where clause $query .= 'WHERE zone = ' . mysql_real_escape_string($_POST['zone']); - also here: $query = 'SELECT * FROM station_tab '; - and mutch more times Commented Sep 26, 2015 at 20:31

2 Answers 2

1

Here is your corrected code:

   $query = 'SELECT * FROM station_tab '; // note the space at the end
   if (!empty($_POST['station_name']) && !empty($_POST['city']) && !empty($_POST['zone'])) {
      $query .= ' WHERE station_name = "' .mysql_real_escape_string($_POST['station_name']) . '" AND city = "' . mysql_real_escape_string($_POST['city']) . '" AND zone = "' . mysql_real_escape_string($_POST['zone']).'"'; // note the = signs and the space before each AND 
   } elseif (!empty($_POST['station_name'])) {
      $query .= ' WHERE station_name = "' . mysql_real_escape_string($_POST['station_name']).'"'; // note the = sign and the space at the beginning
   } elseif (!empty($_POST['city'])) {
      $query .= ' WHERE city = "' . mysql_real_escape_string($_POST['city']).'"'; // note the = sign and the space at the beginning
   } elseif (!empty($_POST['zone'])) {
      $query .= ' WHERE zone = "' . mysql_real_escape_string($_POST['zone']).'"'; // note the = sign and the space at the beginning
   } else {
       echo "Choose at least one option for search";
   }

Take the habit of echoing your $query variable so concatenation does not add any typo mistakes.

Sign up to request clarification or add additional context in comments.

10 Comments

Can I ask why is that space so important? and thank guys I have moved for now. edit now I see why it is important.
Because concatenation just merges two strings. If you don't add a space, the words will be sticked with each others, resulting in a typo mistake.
now it echo error Unknown column 'my input' in 'where clause'
Where does 'my input' comes from?
<td><input type="text" name="city" /></td> this is where I am adding name of the city. So if I add Brno error will be Unknown column 'Brno' in 'where clause'
|
0

in phpmyadmin select the database and then select your table

and in menu above there is a sql menu. you can use this functionality to construct sql queries or debug when there are errors like this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.