wordpress中的数据插入

时间:2017-09-02 09:23:44

标签: php mysql wordpress

这是我的数据库连接的php文件

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

我需要你的帮助。请帮我。 提前致谢

1 个答案:

答案 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 Code

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";
}