2015-09-26 75 views
1

這裏的代碼應該搜索數據庫。但我得到我的表不存在的錯誤。另外我想問問爲什麼如果我推第二次提交按鈕,它只是跳轉到else所以它回聲choose at least.... and also all data from database.謝謝!獲取表不存在與PHP和MySQL

這裏是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']; 
    }  
    } 
    } 

這裏是錯誤消息,當我到城市中的城市加名。

Table 'stanice_tab.station_tabwhere' doesn't exist 
+0

您確定要連接到正確的數據庫嗎? – D4V1D

+0

是的,我是,因爲它回聲從station_tab的所有數據:) – R2D2

+0

所以,當你得到「表不存在」的錯誤? – D4V1D

回答

1

這裏的錯誤是你糾正代碼:

$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"; 
    } 

echo習慣荷蘭國際集團的$query變量,以便串聯不添加任何錯字錯誤。

+0

我可以問爲什麼這個空間如此重要?並感謝我現在已經搬家的人。現在編輯我明白爲什麼它很重要。 – R2D2

+1

因爲連接只是合併兩個字符串。如果你沒有添加空格,這些單詞將會與對方粘在一起,導致錯字錯誤。 – D4V1D

+0

現在它回顯錯誤'未知的列'我的輸入'在'子句'' – R2D2

0

在phpMyAdmin選擇數據庫,然後選擇您的表

,並在菜單上面有一個SQL菜單。您可以使用此功能來構建SQL查詢或調試時,有這樣的