2014-10-27 161 views
0

我試圖通過創建表來將數據插入到Wordpress數據庫中。我創建了表,但是當我嘗試從表單插入數據時,它不插入數據。我已檢查數據庫連接的工作原理,但插入不會發生。有人可以幫助我嗎?這裏是我的代碼: -將數據從自定義表格插入Wordpress數據庫表

<?php 
    require_once('/wp-config.php'); 
    global $wpdb; 

    if(isset($_POST['submit'])){ 
     $wpdb->insert('wp_post_job', array('organizationname' => 
     $_POST['organizationname'], 'post' => $_POST['post'], 'publishfrom' => 
     $_POST['publishfrom'], 'publishupto' => $_POST['publishupto'], 
     'qualification1' => $_POST['qualification1'], 'qualification2' => 
     $_POST['qualification2'], 'qualification3' => $_POST['qualification3'], 
     'qualification4' => $_POST['qualification4'], 'experience1' => 
     $_POST['experience1'], 'experience2' => $_POST['experience2'], 
     'experience3' => $_POST['experience3'], 'training1' => $_POST['training1'], 'training2' => $_POST['training2'], 'training3' => $_POST['training3'], 
     'training4' => $_POST['training4'], 'training5' => $_POST['training5']), 
     array('$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s', '$s')); 
    } 
?> 

<?php 
/* 
Template Name: Form 
*/ 
?> 
<?php global $pc_theme_object; /* Reference theme framework class */ ?> 
<?php get_header(); ?> 

<form action="" id="postjob" method="post"> 
    <table> 
     <tr> 
      <td><label for="organizationname">Organization Name:</label></td> 
      <td><input type="text" name="organizationname" id="organizationname" value="/></td> 
     </tr> 
     <tr> 
      <td><label for="post">Post:</label></td> 
      <td><input type="text" name="post" id="post" value="" /></td> 
     </tr> 
     <tr> 
      <td><label for="publishfrom">Publish From:</label></td> 
      <td><input type="text" name="publishfrom" id="publishfrom" /></td> 
     </tr> 
     <tr> 
      <td><label for="publishupto">Publish Upto:</label></td> 
      <td><input type="text" name="publishupto" id="publishupto" /></td> 
     </tr> 
     <tr> 
      <td><label for="qualification">Qualification:</label></td> 
      <td><input type="text" name="qualification1" id="qualification1" /></td> 
      <td><input type="text" name="qualification2" id="qualification2" /></td> 
      <td><input type="text" name="qualification3" id="qualification3" /></td> 
      <td><input type="text" name="qualification4" id="qualification4" /></td> 
     </tr> 
     <tr> 
      <td><label for="experience">Experience:</label></td> 
      <td><input type="text" name="experience1" id="experience1"/></td> 
      <td><input type="text" name="experience2" id="experience2"/></td> 
      <td><input type="text" name="experience3" id="experience3"/></td> 
     </tr> 
     <tr> 
      <td><label for="training">Training:</label></td> 
      <td><input type="text" name="training1" id="training1" />></td> 
      <td><input type="text" name="training2" id="training2" /></td> 
      <td><input type="text" name="training3" id="training3" /></td> 
      <td><input type="text" name="training4" id="training4" /></td> 
      <td><input type="text" name="training5" id="training5" /></td> 
     </tr> 
     <tr> 
      <td><button type="submit" name="submit">Submit</button></td> 
     </tr> 
    </table> 
</form> 

<?php get_footer(); ?> 

回答

5

替換 '$ S' 與 '%s' 的

使用此

if (isset($_POST['submit'])){ 
    $wpdb->insert('wp_post_job', array(
     'organizationname' => $_POST['organizationname'], 
     'post' => $_POST['post'], 
     'publishfrom' => $_POST['publishfrom'], 
     'publishupto' => $_POST['publishupto'], 
     'qualification1' => $_POST['qualification1'], 
     'qualification2' => $_POST['qualification2'], 
     'qualification3' => $_POST['qualification3'], 
     'qualification4' => $_POST['qualification4'], 
     'experience1' => $_POST['experience1'], 
     'experience2' => $_POST['experience2'], 
     'experience3' => $_POST['experience3'], 
     'training1' => $_POST['training1'], 
     'training2' => $_POST['training2'], 
     'training3' => $_POST['training3'], 
     'training4' => $_POST['training4'], 
     'training5' => $_POST['training5']), 
     array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') 
    ); 
} 
+0

它的工作。謝謝。 – Nikx 2014-10-28 07:18:35

+0

關於格式參數的Wordpress codex說:「如果省略,'$ data'中的所有值都將被視爲字符串,除非'wpdb :: $ field_types'中另有指定。在這種情況下,這似乎是明智的做法。 https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row – 2017-04-10 12:38:06

7

您可以使用此

<?php 

if (isset($_POST['submit'])){ 

    global $wpdb; 


    $tablename=$wpdb->prefix.'post_job'; 

    $data=array(
     'organizationname' => $_POST['organizationname'], 
     'post' => $_POST['post'], 
     'publishfrom' => $_POST['publishfrom'], 
     'publishupto' => $_POST['publishupto'], 
     'qualification1' => $_POST['qualification1'], 
     'qualification2' => $_POST['qualification2'], 
     'qualification3' => $_POST['qualification3'], 
     'qualification4' => $_POST['qualification4'], 
     'experience1' => $_POST['experience1'], 
     'experience2' => $_POST['experience2'], 
     'experience3' => $_POST['experience3'], 
     'training1' => $_POST['training1'], 
     'training2' => $_POST['training2'], 
     'training3' => $_POST['training3'], 
     'training4' => $_POST['training4'], 
     'training5' => $_POST['training5']); 


    $wpdb->insert($tablename, $data); 
} 

?> 
+0

很高興你的代碼工作。我沒有看到任何東西來消毒你的輸入。鑑於您使用$ _POST變量進行直接提交,所以您可能希望考慮進行消毒,否則安全風險相當高。 – 2017-09-07 10:15:07

相關問題