2015-09-26 112 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查询或调试时,有这样的