PHP脚本仅在首次提交后发布到数据库

时间:2013-09-09 00:03:11

标签: php

我将自己的自定义CMS后端构建到我一直在工作的网站之一,并且我的网站功能遇到了一些奇怪的行为。

管理员首先会看到一个表单,其中有两个部分用于填写“横幅”和“第1部分”,然后他/她可以单击屏幕一侧的按钮将新部分附加到页面,这是通过一些jQuery来处理,以将新元素附加到文档。

此处的脚本与往常一样,所有部分都可以填写,但在提交时,脚本会抛出大量错误。如果再次输入信息,表单将按预期提交,并将所有正确的信息提交给数据库。我想知道的是,jQuery是否与我在提交DOM时的行为方式相冲突或者我在PHP脚本中犯了错误?过去几天我一直坚持这一点,任何建议都会非常感激。

这里我们检查要渲染的视图

    <?php if(!isset($_POST['submission'])){
        include_once("includes/getsection_view.php");
    } else if(isset($_POST['submission'])){
        include_once("includes/getsection_view_return.php");
    }
?>

标记

getsection_view.php(默认视图)

<form method="POST" enctype="multipart/form-data" id="theForm">
    <?php 
        $title = "";
        $body = "";

        if(isset($_POST['submit'])){
             for($i=0; $i <=count($nodes); $i++)
             {
                        if(isset($_POST['sectionTitle'.$i])){ $title+$i = htmlentities($_POST['sectionTitle'.$i]);};
                        if(isset($_POST['sectionContent'.$i])){ $body+$i = htmlentities($_POST['sectionContent'.$i]);};
                }
            }
    ?>
        <div id="sectionInfo">
            <div id="sectionWrap">
                <div id="sectionInner">
                    <label class="inst head">Section 1</label>
                    <input class="textOver" type="text" name="sectionTitle1" value="<?php echo $title; ?>" placeholder="Section Title" onfocus="this.select();">
                    <label class="inst ib">Please enter any text you would like associated with the section.</label>
                    <textarea style="margin-top: 3px" name="sectionContent1" value="<?php echo $body; ?>" onfocus="this.select();" placeholder="Section Description"></textarea>
                    <label class="inst ib">Please upload the image associated with this section, .PNG required (588x514px).</label>
                    <input type="file" style="visibility:hidden;" name="sectionImg1" class="upload" />
                    <input type="button" id="fileStyle" class="fSOver fileStyle" value="Upload Section Image!" />
                </div>
            </div>
        </div>          
        <br>
        <div align="center" class="logBut" id="postBut">
            <input style="width: 942px !important; margin-left: -40px !important" type="submit" name="submission" value="Add pump sections to the database" />
        </div>
    </form>
</div>

最后提交时,此脚本运行以将数据输入数据库

  # Get the total section node count (not banner)
     $nodes = 0;
     foreach($_POST as $key => $section){ 
    $nodes++;
     }

    // Get the correct total of apended nodes
     $nodes = (($nodes-3)/2)-1; 
     $i = 1;


if(isset($_POST['submission'])){

// Check for errors on the banner section
if(empty($errors) === true){

    // Perform any last checks
    if(isset($_POST['ptype'])){$serial = $_POST['ptype'];};

    // Banner section validation and input was here...

    // Handle each section
    //*********************
    $sSectionId = 1;

    // Validate each section input
    for($i=1; $i<$nodes+1; $i++){
        if(empty($_POST['sectionTitle'.$i]) || empty($_POST['sectionContent'.$i])){
            $errors[] = 'Please fill in all fields at Section ' .$i;
        }   

        // Image validation
        if (!isset($_FILES['sectionImg'.$i])) {
            $errors[] = 'Please upload a file in PNG format at Section'.$i.'.';
        } else if (isset($_FILES['sectionImg'.$i])) {
            $fileName = $_FILES['sectionImg'.$i]['name'];

                $target_path = IMG_DIR . basename($_FILES['sectionImg'.$i]['name']);

                if(move_uploaded_file($_FILES['sectionImg'.$i]['tmp_name'], $target_path)) {

                } else {
                    $errors[] = "There was an error whilst uploading " . $fileName . " please try again.";
                }

        }

        // Initiate variables for PDO insert
        $sImg = "/" . $fileName;
        $sHeader = $_POST['sectionTitle'.$i];
        $sInfo = $_POST['sectionContent'.$i];

        // If the errors are 100% empty then input the new section to DB
        if(empty($errors)===true){
            $users->add_section($sHeader, $sInfo, $sImg, $serial, $sSectionId);
            $success[] = $nodes+1 . " sections have been added to " . $serial . ".";
        }

        else {
            $errors[] = "It seems something went wrong when trying to add your data to the system, please try again.";
        }

        $sSectionId += 1;   
    }

}
}

正如我之前所说,这里的代码有效,但只有一次调用getsection_view_return.php,它不能与getsection_view.php脚本一起使用,如果有人对我为什么会遇到这种行为有任何想法我会是最感谢任何指针。

如果需要更多代码信息,请提出要求,我会将其包含在编辑中。

注意:如果有人需要完整标记,请检查编辑日志,这是缩小的。

1 个答案:

答案 0 :(得分:1)

我不知道这是否会解决您的主要问题,但我发现代码中可能存在“错误”,例如使用$_POST['submit']代替$_POST['submission'],并建议更换{{1 {},$title+$i对我来说听起来很奇怪的事情:

$body+$i