2

次のフォームが適切に機能し、MySQL DB にデータが入力されました。それ以来、新しいフィールド「タイプ」をデータベースに追加し、それをフォームにも追加しました。ただし、新しいエントリを追加しようとすると、「列名 'タイプ' が無効です」と表示されます。どんな助けでも大歓迎です。

<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('','','','','','','','','','');
 }
4

2 に答える 2

3

typeクエリで列名をエスケープする必要があります。つまりtype、これtypeは MySQL 構文で予約されている作業です。

とにかく、どうやって ` マークをエスケープするのですか?

于 2012-10-23T20:47:50.030 に答える
2

typeMS SQL の予約語です。これは、他の MS SQL 操作で使用されることを意味します。select同様に、またはという名前の列がある場合、問題が発生します。group

括弧を使用して予約語をエスケープできます。[]

$SQL = "INSERT INTO JobPosting (posted, ends, [type], position, location, hours, pay, benefits, description) VALUES ('$posted', '$ends', '$type', '$position', '$location', '$hours', '$pay', '$benefits', '$description')"; 
于 2012-10-23T20:50:23.290 に答える