首页 > 解决方案 > 未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交

问题描述

注意:未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交

<?php
/**
 * Plugin Name:       Form Insert DB
 * Plugin URI:        http://solutionshint.com
 * Description:       Just Insert Data into Custom Form
 * Version:           1.0
 * Author:            SolutionsHint
 * Author URI:        http://solutionshint.com
 */

function custom_form() {
?>
    <form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
         <label for name=""> Name:</label><br>
         <input type = "text" name = "name" id = "name" placeholder = "Enter Name">
         <label for name=""> City:</label><br>
         <input type = "text" name = "city" id = "city" placeholder = "Enter City">
         <label > State:</label><br>
         <input type = "text" name = "state" id = "state"  placeholder = "Enter State">
         <label> Age:</label><br>
         <input type = "text" name = "age" id = "age"  placeholder = "Enter Age">
         <input type = "submit" name = "submit" value = "Insert">
    </form
<?php
}

// add_shortcode('display', 'custom_form');

if($_POST['submit']) {
    global $wpdb;
    $table_name ='student';
    $name = $_POST['name'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $age = $_POST['age'];

    $success = $wpdb->insert("student", array(
       "name" => $name,
       "city" => $city,
       "state" => $state,
       "age" => $age ,
    ));
    if($success) {
        echo ' Inserted successfully';
    } else {
        echo 'not';
    }
}
?>

这是我的代码,我是 php 新手,我正在开发一个插件,可以将表单数据保存到数据库中,但我得到了错误

注意:未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交

标签: phpwordpress

解决方案


试试这个代码。你需要使用if(isset($_POST['submit'])) {

<?php
/**
 * Plugin Name:       Form Insert DB
 * Plugin URI:        http://solutionshint.com
 * Description:       Just Insert Data into Custom Form
 * Version:           1.0
 * Author:            SolutionsHint
 * Author URI:        http://solutionshint.com
 */

    function custom_form() {
    ?>
        <form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
             <label for name=""> Name:</label><br>
             <input type = "text" name = "name" id = "name" placeholder = "Enter Name">
             <label for name=""> City:</label><br>
             <input type = "text" name = "city" id = "city" placeholder = "Enter City">
             <label > State:</label><br>
             <input type = "text" name = "state" id = "state"  placeholder = "Enter State">
             <label> Age:</label><br>
             <input type = "text" name = "age" id = "age"  placeholder = "Enter Age">
             <input type = "submit" name = "submit" value = "Insert">
        </form
    <?php
    }

    // add_shortcode('display', 'custom_form');

    if(isset($_POST['submit'])) {
        global $wpdb;
        $table_name ='student';
        $name = $_POST['name'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $age = $_POST['age'];

        $success = $wpdb->insert("student", array(
           "name" => $name,
           "city" => $city,
           "state" => $state,
           "age" => $age ,
        ));
        if($success) {
            echo ' Inserted successfully';
        } else {
            echo 'not';
        }
    }
?>

推荐阅读