php - 获取表不存在于 php 和 mysql

标签 php mysql

这里的这段代码应该搜索数据库。但我收到错误消息说我的表不存在。我还想问为什么如果我第二次按下提交按钮它只是跳转到 else 所以它回显 choose at least.... 以及数据库中的所有数据。谢谢!

这是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

最佳答案

这是您更正后的代码:

   $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 变量的习惯,这样串联就不会添加任何打字错误。

关于php - 获取表不存在于 php 和 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32801654/

相关文章:

php - woocommerce_order_status_completed 未触发

php - 在 PHP 中为用户创建一个 CSV 文件

MySQL DAYOFWEEK() - 我的一周从星期一开始

PHP 重置 $POST 值并列出所有结果

php - 如何在php页面的选项列表中显示sql行

PHP 提供闭包来确定数组中项目的唯一性

javascript - 试图在一页上获取多个 Google 图表,但没有成功

javascript - 为什么我的 Highcharts 没有加载到后续的 jquery-ajax 调用中?

PHP 代码返回未知列错误

php - 教义迁移问题