0

当我提交表单时,我遇到了第一个验证错误。没有数据发布。

我是所有这些 stackOverflow 的新手,也是所有数据库场景的新手。为了得到我所拥有的,我使用了一些 TUT 和书籍。

希望可以有人帮帮我。

    $itemid = $_GET['page_id'];
$itemid = mysql_real_escape_string($itemid);

    //get data from database that needs editing
    $sql = mysql_query("SELECT * FROM content WHERE `page_id`='{$itemid}'")or die(mysql_error());
        //if(!$sql) die ("Database access failed" . mysql_error());

        if(isset($_POST['submit'])){
            //start validation
            //check fields are not empty

            if(empty($pagetitle)) {
                $error['page_title'] = 'enter a title.';
                }

                $pagecontent = trim($_POST['page_content']);

                    if(empty($pagecontent)){
                        $error['page_content'] = 'Please enter your content.';
                    }

                        //If validation is ok... cary on.. do this
                        if (!$error) {
                            $pageid = $_POST['page_id'];
                            $pagetitle = $_POST['page_title'];
                            $pagecontent = $_POST['page_content'];

                        //Update items
                            $sql = "UPDATE content SET page_title ='$pagetitle', page_content ='$pagecontent' WHERE page_id='$itemid'";
                                $resultupdate = mysql_query($sql)or die (mysql_error());

                                //Success Message

                                    echo "Your site is now updated";
                                        }//close if !error
                                    }//close if form submit

                            //input validation checks input not empty
                            if (isset($error['page_title'])) {
                                echo "<p><span class=\"warning\">" . $error['page_title']."</span><p> ";
                            }

                            if (isset($error['page_content'])) {
                                echo "<p><span class=\"warning\">" . $error['page_content']."</span><p> ";
                            }
                            ?>

<div>
    <?php while ($row = mysql_fetch_object($sql)) { ?>

        <form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
            <input type="hidden" name="page_id" value="<?php echo $row->page_id; ?>" />
            <div class="edit-title">
                <h2><label>Page Title</label></h2>
                <p><textarea name="page_title"><?php echo $row->page_title; ?></textarea></p>
            </div><!-- end edit title -->

            <div class="edit-content">
                <h2><label>Page Content</label></h2>
                <p><textarea name="page_content"><?php echo $row->page_content; ?></textarea></p>
            </div><!-- end edit content -->

            <div class="submit-form">
                <input type="submit" name="submit" value="Update" />
            </div>
        </form>

    <?php } ?>
</div>
4

1 回答 1

0

你检查是否$pagetitle存在但你没有初始化它,你应该把:

if(empty($_POST['page_title'])) { ... }

编辑 :

if(isset($_POST['submit'])){
        //start validation
        //check fields are not empty

        if(empty($_POST['page_title'])) {
            $error['page_title'] = 'enter a title.';
        }

        $pagecontent = trim($_POST['page_content']);

        if(empty($pagecontent)){
            $error['page_content'] = 'Please enter your content.';
        }

        //If validation is ok... cary on.. do this
        if (!$error) {
            $pageid = $_POST['page_id'];
            $pagetitle = $_POST['page_title'];
            $pagecontent = $_POST['page_content'];

            //Update items
            $sql = "UPDATE content SET page_title ='$pagetitle', page_content ='$pagecontent' WHERE page_id='$itemid'";
            $resultupdate = mysql_query($sql)or die (mysql_error());

            //Success Message

            echo "Your site is now updated";
        }//close if !error
    }//close if form submit
于 2013-05-16T09:28:53.663 回答