php - 无法使用 mysqli (PHP) 在表中插入行

标签 php mysql mysqli syntax reserved-words

我想在表格中插入一行,但它给我语法错误。

这是 HTML 代码:

    <form method="post" action="" >
    <!--form fields-->

    <div class="control-group">

    <div class="customer_data">
    <div class="controls" id="id_milesPerDay">

    <label for="ICAO" class="left-text">ICAO:</label>

    <input type="text" id="ICAO" style='text-transform:uppercase'  maxlength="4" name="ICAO" placeholder="" class="input-xlarge" required>
     </div>

    <div class="controls">
    <label for="IATA" class="left-text">IATA:</label>

    <input type="text" name="IATA" maxlength="3" style='text-transform:uppercase' id="IATA" placeholder="" class="input-xlarge" required>
    </div>

    <div class="controls">
    <label for="airportname" class="left-text">Airport Name:</label>

    <input type="text" name="airport_name" maxlength=""  id="Airport_Name" placeholder="" class="input-xlarge" required>
    </div>

    <div class="controls">
    <label for="coordinates" class="left-text">Coordinates:</label>

    <input type="text" name="coordinates" maxlength=""  id="Coordinates" placeholder="" class="input-xlarge" required>
    </div>

    <div class="controls">
    <label for="Time_zone_UTC" class="left-text">Time Zone UTC:</label>

    <input type="text" name="Time_zone_UTC" maxlength="3"  id="Time_zone_UTC" placeholder="" class="input-xlarge" required>
    </div>

    <div class="controls">
    <label for="DST" class="left-text">DST:</label>

    <input type="checkbox" name="dst" value="other"  id="DST" placeholder="" class="input-xlarge">
    </div>

    <div class="controls text">
    <label for="utc" class="left-text">UTC:</label>

    <input type="text" name="utc" maxlength="3"   id="UTC" placeholder="" class="input-xlarge">
    </div>

    <div class="controls text">
    <label for="From_date" class="left-text">From Date:</label>

    <input type="date" name="from_date" maxlength=""   id="fromdate" placeholder="" class="input-xlarge">

    </div>
    <div class="controls text">
    <label for="To_date" class="left-text">To Date:</label>

    <input type="date" name="to_date" maxlength=""   id="todate" placeholder="" class="input-xlarge">
    </div>  
    <!-- Button -->
    <div class="control-group">
    <div class="controls">
    <input type="submit" value="Submit" id="Submit" class="btn btn-success" name="Submit">  
    </div>
    </div>
    </div>
    <div class="customer_aircraft">
    <div class="controls">
    <label for="country_code" class="left-text">Country Code:</label>

    <input type="text"  name="country_code" maxlength="2" style='text-transform:uppercase'  id="countrycode" placeholder="" class="input-xlarge" required>
    </div>

    <div class="controls">
    <label for="Remarks" class="left-text">Remarks:</label>

    <textarea cols="25" rows="7"  name="remarks"  id="remarks" placeholder="" class="input-xlarge"></textarea> 

    </div>

    <div class="controls">
    <label for="Country" class="left-text">Country:        </label>

    <input type="text"  name="country" maxlength=""   id="country" placeholder="" class="input-xlarge" required>
    </div>
    </div>

   </div>       
    </form>
    </body>
    </html>

PHP 代码:

     <?php

     $link=require ("Connection.php");

     error_reporting(E_ALL); ini_set('display_errors', 1);


     if(isset($_POST['Submit']))
      {
   $ICAO= mysqli_real_escape_string($link,$_POST['ICAO']);
   $IATA= mysqli_real_escape_string($link,$_POST['IATA']);
   $Airport_Name= mysqli_real_escape_string($link,$_POST['airport_name']);
   $coordinates= mysqli_real_escape_string($link,$_POST['coordinates']);
   $Time_zone_UTC= mysqli_real_escape_string($link,$_POST['Time_zone_UTC']);
   $DST= mysqli_real_escape_string($link,$_POST['dst']);
   $UTC= mysqli_real_escape_string($link,$_POST['utc']);

   $from_date = mysqli_real_escape_string($link,$_POST['from_date']);

   $to_date =   mysqli_real_escape_string($link,$_POST['to_date'];


   $country_code= mysqli_real_escape_string($link,$_POST['country_code']);
   $remarks= mysqli_real_escape_string($link,$_POST['remarks']);
   $Country= mysqli_real_escape_string($link,$_POST['country']);


   //inserting records//

     $sql="INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('$ICAO','$IATA','$Airport_Name','$coordinates','$Time_zone_UTC','$DST','$UTC',STR_TO_DATE('$from_date', '%m/%d/%y'),STR_TO_DATE('$to_date', '%m/%d/%y'),'$country_code','$remarks','$Country')";
    if(mysqli_query($link, $sql))
    {
    echo "Records added successfully.";
    }
    else
    {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    // close connection
    mysqli_close($link);




    } 

    ?>

这是我收到的错误:

ERROR: Could not able to execute INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','islam','+9','other','+8',STR_TO_DATE('2015-05-26', '%m/%d/%y'),STR_TO_DATE('2015-05-20', '%m/%d/%y'),'op','p','Pakistan'). 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 'From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','i' at line 1

最佳答案

转义(至少)列名中的保留字。

... `From`, `To`, ...

MySQL 保留字的完整列表在 http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html 中.

关于php - 无法使用 mysqli (PHP) 在表中插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183652/

相关文章:

javascript - PHP 和 MySQL : Update not working with Foreach Loop

php - 产品、变体和库存-什么是设计数据库的最佳方式

php - 绑定(bind)参数和绑定(bind)结果不返回item - PHP MySql

php - 棘手的 MySQL 插入和更新

php - 如何在没有 "bind_param"的情况下进行更新

php - 已连接页面PHP的mysqli_query连接

php - 我如何使用基于 PHP/MySQL 的脚本来禁止网站的 IP block ?

php - 使用ajax获取值

Mysql全文搜索 bool 模式按相关性排序而不是我正在寻找的顺序(短语匹配)

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows等…期望参数1为资源或结果