未定义变量:提交表单时使用conn

时间:2019-11-17 09:27:09

标签: php

我正在尝试保存表单中的数据,但是由于某种原因,每当我提交表单时,我都会不断获取

  

注意:未定义的变量:第54行的E:\ Laptop \ Wamp \ www \ cms \ admin \ pages \ menus \ add_menu.php中的conn

我的db.php

<?php
    $db['db_host'] = "localhost";
    $db['db_user'] = "root";
    $db['db_pass'] = "";
    $db['db_name'] = "cms";

    // Gonna turn the db[] into a constant
    // Makes it more secure
    foreach($db as $key => $value)
    {
        define(strtoupper($key), $value);
    }

    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if($conn)
    {
        echo "we are connected";
    }    
?>

这是admin_header.php

<?php
    include "../includes/db.php";
?>

这是我的menus.php。这是我包含标题的地方,并且取决于您通过$_GET['source]单击的内容,将带您进入必要的页面。

<?php
    include "includes/admin_header.php";
?>

<script src="<?php echo $link ?>/admin/js/menus.js"></script>

<body class="fixed-nav sticky-footer bg-dark" id="page-top">
    <!-- Navigation-->
    <?php
        include "includes/admin_nav.php";
    ?>

    <div class="content-wrapper">
        <div class="container-fluid">
            <!-- BEGIN HEADING -->
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">
                        Menus 
                    </h1>
                </div>
            </div>
            <!-- END HEADING -->

            <!-- BEGIN DISPLAYING CRUD PAGES -->
            <?php
                //IF $_GET HAS SOURCE GRAB THE SOURCE VARIABLE ELSE GRAB NOTHING
                if(isset($_GET['source']))
                {
                    $source = $_GET['source'];
                }else{
                    $source = "";
                }

                //DEPENDING ON WHAT THE $_GET['SOURCE'] IS SWITCH TO THE NECESSARY CASE.
                switch($source)
                {
                    case 'add_menu';
                        include "pages/menus/add_menu.php";
                    break;

                    case 'edit_menu';
                        include "pages/menus/edit_menu.php";
                    break;

                    default:
                        include "pages/menus/index.php";
                }
            ?>
            <!-- END DISPLAYING CRUD PAGES -->

        </div>
    </div>
</body>

<?php
    include "includes/admin_footer.php";
?>

这是我的add_menu.php

<?php

    if(isset($_POST['menu_title']))
    {
        $menu_title = $_POST['menu_title'];
        $menu_type = $_POST['type'];
        $menu_parent_id = $_POST['selectParentMenu'];
        $image = $_POST['files'];

        $menu_image = '['.$image.']';

        if(isset($_POST['main_menu']))
        {
            $main_menu = "1";
        }else{
            $main_menu = "0";
        }

        if(isset($_POST['side_menu']))
        {
            $side_menu = "1";
        }else{
            $side_menu = "0";
        }

        $query = "INSERT INTO menus(title, type, image, parent_id, main_menu, side_menu) ";
        $query .= "VALUES('{$menu_title}', '{$menu_type}', '{$menu_image}', '$menu_parent_id', '$main_menu', '$side_menu') ";

        // THIS IS WHERE THE ERROR IS HAPPENING
        $create_menu_query = mysqli_query($conn, $query);

        //THIS FUNCTION CHECKS TO SEE IF THERE IS NO ERRORS WITH THE QUERY
        //THIS FUNCTION COMES FROM FUNCTIONS.PHP
        confirmQuery($create_menu_query);

        //THIS REDIRECTS THE PAGE AFTER SAVING
        header("Location: menus.php");
    }
?>

<form action="" method="POST" enctype="multipart/form-data">
    <div class="form-group">
        <label for="selectParentMenu">Parent Menu</label>

        <select name="selectParentMenu" id="selectParentMenu" class="form-control">
            <option value="0">Please Select</option>

            <?php
                $query = "SELECT * FROM menus";
                $menus = mysqli_query($conn, $query);
                confirmQuery($menus);

                while($row = mysqli_fetch_assoc($menus))
                {
                    $menu_id = $row['id'];
                    $menu_title = $row['title'];

                    echo "<option value='$menu_id'>";
                    echo    $menu_title;
                    echo "</option>";
                }
            ?>
        </select>
    </div>

    <div class="form-group">
        <label for="menu_title">Menu Title</label>
        <input type="text" class="form-control" name="menu_title" id="menu_title">
    </div>

    <div class="form-group">
        <label for="menu_image">Image</label>
        <div id="fileuploader">Upload</div>
    </div>

    <div class="form-group">
        <div class="form-check">
            <input type="radio" class="form-check-input" name="type" id="type_home" value="home">
            <label for="type_home">Home</label>
        </div>

        <div class="form-check">
            <input type="radio" class="form-check-input" name="type" id="type_about" value="about">
            <label for="type_about">About</label>
        </div>

        <div class="form-check">
            <input type="radio" class="form-check-input" name="type" id="type_product" value="product">
            <label for="type_product">Product</label>
        </div>

        <div class="form-check">
            <input type="radio" class="form-check-input" name="type" id="type_contact" value="contact">
            <label for="type_contact">Contact</label>
        </div>
    </div>

    <div class="form-group">
        <div class="form-check">
            <input type="checkbox" name="main_menu" id="main_menu_checkbox">
            <label for="main_menu_checkbox">Main Menu</label>
        </div>

        <div class="form-check">
            <input type="checkbox" name="side_menu" id="side_menu_checkbox">
            <label for="side_menu_checkbox">Side Menu</label>
        </div>
    </div>

    <div class="form-group">
        <button type="button" name="create_menu" class="btn btn-dark btn-lg btn-block create_menu">Submit</button>
    </div>
</form>

这是我的menus.js

$(document).ready(function()
{
    var getUrl = "http://cms.test";

    $("#fileuploader").uploadFile({
        url: getUrl+"/admin/includes/upload.php",
        multiple: true,
        dragDrop: true,
        showPreview: true,
        previewWidth: "100px",
        showDelete: true,
        fileName:"myfile"
    });

    $("#fileuploader").on('drop', function(e){
        e.preventDefault();

        var files_list = e.originalEvent.dataTransfer.files;

        var filename_list = [];
        for(x = 0; x < files_list.length; x++)
        {
            var filename = files_list[x].name;
            filename_list.push(filename);
        }

        $(this).trigger('got-dropped', [filename_list]);
    });

    $('#fileuploader').on('got-dropped', function(event, files){
        saveForm(files);
    });
});

function saveForm(files_list)
{
    $(".create_menu").click(function(){
        var menu_title = $('input#menu_title').val();
        var type = $("input[name*='type']").val();
        var main_menu_checkbox = $('input#main_menu_checkbox').val();
        var side_menu_checkbox = $('input#side_menu_checkbox').val();
        var files = files_list;

        var selectParentMenu = $('#selectParentMenu').val();

        var dataString = 'menu_title='+menu_title+'&type='+type+'&main_menu_checkbox='+main_menu_checkbox+'&side_menu_checkbox='+side_menu_checkbox+'&selectParentMenu='+selectParentMenu+'&files='+files;

        $.ajax({
            type: 'post',
            url: 'pages/menus/add_menu.php',
            data: dataString,
            success: function(){
                $("#message").html("<h2>It has been submitted</h2>");
            }
        })
    });
}

0 个答案:

没有答案