首页 > 解决方案 > 根据表单数据和传递的值执行插入的函数

问题描述

短(est)版本:我创建了一个简单的 CMS,允许用户从 4 个模板中进行选择。每个模板都有不同的面板类型。对于这个例子,我展示了一个只有一个整页面板的整页模板。

基本上,面板提供了一个 TinyMCE 实例,以允许用户为面板创建文本/图像内容。

流程是:内容->(分配给)->面板->(分配给)->页面->(分配给)->显示

所以在这种情况下,用户选择一个通过 URL 传递值的链接,在这种情况下,值为 1。这告诉 templates.php 包含 fullWidth.php

所以 templates.php 有一个带有提交按钮的保存表单,加载的 fullWidth.php 文件有面板模板,它为我们提供面板类型的编号以及文本区域中的内容。

我的问题是,我需要完全实现保存页面的功能(通过代理保存内容和面板的记录)

因此,使用下面的代码,我基本上会插入 3 个不同的表,但该panels表正在插入外键;新创建的pages条目以及新插入的content条目的 ID。

我知道其中大部分只是将一些表单值插入到数据库中,但这里有两个主要障碍:

  1. 如何插入不在表单中的值,例如来自 fullwidth.php 的内容和 panel_type ID 以及在 templates.php 中传入的 $value
  2. 如何创建一个主要插入功能,并能够获取用作面板外键的 2 个不同 ID。

对于我发布的代码示例(假设用户正在选择<options>我在下面使用的硬编码),我将插入:

保存内容页面.php

//This is complete pseudo code, I'm not sure how the syntax would change for the different types
$title= $_POST['$title'];
$pageType= $_POST['$value'];
$displayId = $_POST['#areaSelect'];
$start_time = now();
$end_time = $_POST['#datePicker'];
$slide_order = $_POST['#orderSet'];
$duration = $_POST['#durationSet'];

$sql="INSERT INTO pages(title, page_type_id, display_id, start_time, end_time, slide_order, duration) 
        VALUES ('$title','$pageType','$displayId','$start_time','$end_time','$slide_order','$duration')";

//Here I need to pass the content from the included fullwidth.php to get the textarea content and the panel_type_id
$content = $_POST['textArea']

$sql = "INSERT INTO content(content)
        Values('$content')";

if(#id = FullPage){
$panel_type = 1;
}

$sql = INSERT INTO panels (panel_type_id, page_id, cont_id)
        VALUES ('$panel_type', /*ID that was created from 'pages' insert*/, /*ID that was created from 'content' INSERT*/)

如何创建执行必要插入的函数?

代码:

模板.php

    <!-- check GET 'value' -->
    <?php $value = isset($_GET['value']) ? $_GET['value'] : 1;?>

    <!-- If value is 1 then load the full page template class -->
    <?php if($value == 1){?>
    <?php include 'class/fullWidth.php'?>

    <!-- Submit button that links to modal with a 'save page' form inside -->
    <div class="modal fade" id="savePageModal" tabindex="-1" role="dialog" aria-labelledby="savePageLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="savePageLabel">Page Details:</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button> 
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <!-- this is the URL value i.e. templates.php?value=1 and should insert into pages.page_type_id -->
                        <label>Page Type: <?php echo $value;?></label>
                        </br>

                        <!-- This is page title, should map to pages.title -->
                        <label for="addTitle">Page Title:</label>
                        <input class="form-control" id="addTitle">
                        </input>

                        <!-- This shows the displays, will save the ID of the selected option to pages.display_id -->
                        <label for="areaSelect">Select An Area</label>
                        <select class="form-control" id="areaSelect">

                            <option>4</option>


                        </select>

                        <!-- Input for seconds, will save to pages.duration -->
                        <label for="durationSet">Set A Duration (in seconds)</label>
                        <input class="form-control" id="durationSet">
                        </input>

                        <!-- User selects order of slide -->
                        <label for="orderSet">Set Slide Order</label>
                        <select class="form-control" id="orderSet">
                            <option>3</option>
                        </select>

                        <!-- This datepicker allows user to pick a date and time, will save as TIMESTAMP to pages.end_time -->
                        <label for="expirationSelect">Set Expiration Date/Time</label>
                        <div class="form-group">
                          <div class="datepick input-group date" id="datetimepicker" data-target-input="nearest">
                            <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker"/>
                            <span class="input-group-addon" data-target="#datetimepicker" data-toggle="datetimepicker">
                            <span class="fa fa-calendar"></span>
                            </span>
                          </div>
                        </div>


                    </div>
                </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                  </div>
                </div>
            </div>
        </div>
    </div>

全宽.php

    <!-- This file is loaded based on page type ($value) from templates.php
        It loads an HTML template which has a main div houseing the panel_type and it has
        a textarea that houses the content -->

    <div class="row middle">
        <div class="col-lg-12 fullWidth">
            <!-- This Div ID is 1 which maps to the panel_type of this div, so upon saving It should insert to panels.panel_type_id a value of 1 -->
            <div class="fullContent" id="fullPage" style="background-color: white; height: 100%;">
                <!-- THis is simply a modal that givees the user an interface to use TinyMCE in order to fill the content of mytextarea3 -->
                <div class="modal fade bd-example-modal-lg" id="fullModal" tabindex="-1" role="dialog" aria-labelledby="fullLabel" aria-hidden="true">
                    <div class="modal-dialog modal-lg" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                              <h5 class="modal-title" id="fullModal">Content Library:</h5>
                              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>
                            </div>
                            <div class="modal-body">
                                <h3>Create your own content</h3>
                                <form id="form-data3" method="post">
                                    <!-- This is the text area that should be saved to content.content -->
                                  <textarea id="mytextarea3"></textarea>
                                  <input type="submit" value="Get Data">
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>
                            </div>
                       </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

标签: phphtml

解决方案


推荐阅读