php - mysql PHP 查询中的列名无效

标签 php sql-server database

我的以下表单正常工作并填充 MySQL 数据库。此后,我在数据库中添加了一个新字段“类型”,并将其添加到表单中。但是现在,当我尝试添加新条目时,它显示“无效的列名‘类型’”。任何帮助将不胜感激。

<script>
      $(document).ready(function() {
        $("#datepicker").datepicker();
      });
      </script>

  <script>
$(document).ready(function() {
$("#datepicker2").datepicker();
});
</script>

 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

  <form action="" method="post">
 <div>
 <label for="Posted"><strong>Posted: </strong> </label>
 <input id="datepicker"  name="posted" value="<?php echo $Posted; ?>" /><br/><br/>

 <label for="Ends"><strong>Ends: </strong> </label>
 <input id="datepicker2"  name="ends" value="<?php echo $Ends; ?>" /><br/><br/>

 <label for="Position"><strong>Position: </strong></label>
  <input type="text" name="position" value="<?php echo $Position; ?>" /><br/><br/>

  <label for="Location"><strong>Location: </strong> </label>
 <select name="location">
  <option value=" ">Select...<?php echo $Location; ?></option>
  <option value="Fargo">Fargo</option>
  <option value="Grand Forks">Grand Forks</option>
</select><br/><br/>

<label for="Application Type"><strong>Application Type: </strong> </label>
  <select name="type">
  <option value=" ">Select...<?php echo $Type; ?></option>
  <option value="Driver">Driver</option>
  <option value="Employee">Employee</option>
</select><br/><br/>

 <label for="Hours"><strong>Hours: </strong> </label>
 <input type="text" name="hours" value="<?php echo $Hours; ?>" /><br/><br/>

 <label for="Pay"><strong>Pay: </strong> </label>
 <input type="text" name="pay" value="<?php echo $Pay; ?>" /><br/><br/>

 <label for="Benefits"><strong>Benefits: </strong> </label>
 <textarea cols="60" rows="2" name="benefits" value="<?php echo $Benefits; ?>" ><?php echo $Benefits; ?></textarea><br/><br/>

 <label for="Description"><strong>Description: </strong> </label>
 <textarea cols="60" rows="3" name="description" value="<?php echo $Description; ?>" ><?php echo $Description; ?></textarea><br/><br/>


 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }




 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid

 function ms_escape_string($data) {
        if ( !isset($data) or empty($data) ) return '';
        if ( is_numeric($data) ) return $data;

        $non_displayables = array(
            '/%0[0-8bcef]/',            // url encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',             // url encoded 16-31
            '/[\x00-\x08]/',            // 00-08
            '/\x0b/',                   // 11
            '/\x0c/',                   // 12
            '/[\x0e-\x1f]/'             // 14-31
        );
        foreach ( $non_displayables as $regex )
            $data = preg_replace( $regex, '', $data );
        $data = str_replace("'", "''", $data );
        return $data;
    }

    ms_escape_string($_POST);


     $posted=$_POST['posted'];
     $ends=$_POST['ends'];
     $type=$_POST['type'];
     $position=$_POST['position'];
     $location=$_POST['location'];
     $hours=$_POST['hours'];
     $pay=$_POST['pay'];
     $benefits=$_POST['benefits'];
     $description=$_POST['description'];

 // check to make sure all fields are entered
 if ($posted == '' || $ends == '' || $type == '' || $position == '' || $location == '' || $hours == '' || $pay == '' || $benefits == '' || $description == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if any fields are blank, display the form again
 renderForm($posted, $ends, $type, $position, $location, $hours, $pay, $benefits, $description, $error);
 }
 else
 {
 // save the data to the database
    $SQL = "INSERT INTO JobPosting (posted, ends, type, position, location, hours, pay, benefits, description) VALUES ('$posted', '$ends', '$type', '$position', '$location', '$hours', '$pay', '$benefits', '$description')";

     $result = mssql_query($SQL) 
        or die (mssql_get_last_message());  

 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','','');
 }

最佳答案

您需要在查询中转义列名type,即type,因为type是MySQL sytax中的保留工作。

你到底如何逃脱`标记?

关于php - mysql PHP 查询中的列名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038942/

相关文章:

database - migrator.net vs fluentmigrator vs migsharp

php - 在两个网站之间共享 session

php - 如何使用 volley api 将 android 应用程序连接到本地服务器

php - 制作不带 json_encode 的 json 字符串

php - SQL - 执行更新的最快方法

sql - 当比较值由两个单独的字段组合时,是否可以使用单个 SQL 获取表中的最小(最大)行?

sql - 从另一个表的行及其属性创建表

sql - 按mysql工作批中的限制记录进行分组

sql-server - 如何避免 SQL 作业中出现 "Warning: String or Binary data would be truncated"导致失败

iphone - 如何在Iphone中制作RTA分析仪