2017-09-02 76 views
1

這是數據庫連接我的PHP文件數據插入在WordPress

Connection.php

<?php 
$connection = mysqli_connect('localhost', 'root', ''); 
if (!$connection){ 
    die("Database Connection Failed" . mysqli_error($connection)); 
    } 
     $db = mysqli_select_db($connection, 'wp'); 
    if (!$db){ 
     die("Database Selection Failed" . mysqli_error($connection)); 
    } 
    ?> 

這是形式插入記錄到數據庫

add.php

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

    <html> 
    <head> 

    </head> 
     <body> 
    <table> 
    <form action="?php echo $_SERVER["PHP_SELF"];?" method="POST" > 
    <tr><td>Name</td><td><input type="text" name="name"><br></td></tr> 

    <tr><td>Designation</td> 
    <td><select name="des"> 
    <option value="ADMN. OFFICER">ADMN. OFFICER</option> 
     <option value="MANAGER">MANAGER</option> 
     <option value="SUPERVISER">SUPERVISER</option> 
    <option value="DTP DESIGNER">DTP DESIGNER</option> 
     <option value="MACHINE OPERATOR">MACHINE OPERATOR</option> 
     <option value="BINDER">BINDER</option> 
    <option value="BINDING ASST.">BINDING ASST.</option> 
    <option value="OFFICE ASSISTANT">OFFICE ASSISTANT</option> 
    </select></td></tr> 
     <tr><td>Salary:</td><td><input type="text" name="sal"></td></tr> 
    <tr><td>Staff_ID:</td><td><input type="text" name="staff_id"></td></tr> 


    <tr colspan="2"><td><input type="submit" name="submit" value="add"></td> 
    </tr> 
    </table> 
     <a href="http://localhost/wp/view-staff/">View</a> 
    </form> 
    </body> 
    </html> 
     <?php 
     include('connection.php'); 
global $wpdb; 

    if (isset($_POST['submit'])){ 
    $wpdb->insert('staff', array(
    'name' => $_POST['name'], 
    'des' => $_POST['des'], 
    'sal' => $_POST['sal'], 
    'staff_id' => $_POST['staff_id'])); 

     echo "submiteed"; 

    } 
?> 


    <?php get_footer(); ?> 

雖然我給數據的形式,它顯示找不到網頁

page not found

當我點擊按鈕沒有提交任何數據,它的工作原理,並在表中存儲爲空記錄

empty submission

我需要你的幫助。請幫幫我。 預先感謝

+0

看跌形態下表。您的表格標籤在桌子下但在表 –

回答

0

你絕對應該避免https://codex.wordpress.org/WordPress_Query_Vars#Query_variables

使用最新的代碼

<table> 
     <form method="POST" > 
     <tr><td>Name</td><td><input type="text" name="uname"><br></td></tr> 

     <tr> 
      <td>Designation</td> 
      <td> 

       <select name="des"> 
        <option value="ADMN. OFFICER">ADMN. OFFICER</option> 
        <option value="MANAGER">MANAGER</option> 
        <option value="SUPERVISER">SUPERVISER</option> 
        <option value="DTP DESIGNER">DTP DESIGNER</option> 
        <option value="MACHINE OPERATOR">MACHINE OPERATOR</option> 
        <option value="BINDER">BINDER</option> 
        <option value="BINDING ASST.">BINDING ASST.</option> 
        <option value="OFFICE ASSISTANT">OFFICE ASSISTANT</option> 
       </select> 

      </td> 

     </tr> 
     <tr><td>Salary:</td><td><input type="text" name="sal"></td></tr> 
     <tr><td>Staff_ID:</td><td><input type="text" name="staff_id"></td></tr> 
     <tr colspan="2"><td><input type="submit" name="submit" value="add"></td> 
     </tr> 
     <a href="http://localhost/wp/view-staff/">View</a> 
     </form> 
    </table> 

PHP代碼

global $wpdb; 
if (isset($_POST['submit'])){ 
    $wpdb->insert('staff', array(
    'name' => $_POST['uname'], 
    'des' => $_POST['des'], 
    'sal' => $_POST['sal'], 
    'staff_id' => $_POST['staff_id'])); 

    echo "submiteed"; 
} 
+0

後關閉不起作用。請給出其他選項 – gayathri

+0

查看更新代碼@gayathri –

+0

不工作@pawan – gayathri