$ _POST数组空php更新失败

时间:2012-12-07 00:48:15

标签: php

我正在尝试构建包含2个页面的小型网站的管理员端:index.php和更新php。在index.php上我运行查询,用数据库中的数据填充html表单,工作正常。 然后我通过$ _POST将数据发送到update.php页面,在那里我尝试将这些值转换为变量,然后进行更新查询。哪个失败了。我怀疑$ _POST数组有问题 - 有些值搞砸了或空了,但我不明白为什么。 这是index.php的代码:

<?php
if (!isset($page_id)) {

echo " <p>Please select page to be edited:</p>";

$query = mysql_query("SELECT page_id, title FROM pages");

$res = mysql_fetch_array($query);

do {

printf("<p><a href='index.php?page_id=%s'>%s</a></p>", $res['page_id'], $res['title']);
} while ($res = mysql_fetch_array($query));
} else { $query = mysql_query("SELECT *  FROM pages WHERE page_id = '$page_id'");
$res = mysql_fetch_array($query);



require_once 'parts/form.php';}
?>

这是update.php的代码:

<?php
//Here I try to get POST values and assign them to variables for update
//Ths is validation that those values are not empty, 
require_once 'parts/guard.php';

if (isset($_POST['page_id'])) {
$page_id = $_POST['page_id'];
}

if (isset($_POST['title'])) {
$title = $_POST['title'];
}

if ($title == '') {
unset($title);
}


if (isset($_POST['description'])) {
$description = $_POST['description'];
}

if ($description == '') {
unset($description);
}


if (isset($_POST['keywords'])) {
$keywords = $_POST['keywords'];
}

if ($keywords == '') {
unset($keywords);
}


if (isset($_POST['text'])) {
$text = $_POST['text'];
}

if ($text == '') {
unset($text);
}

//variables are set 

require_once 'parts/meta.php';
?>
<?php
//Here is all the values exist, the query is executed. 
//Obviousely this query works in phpmyadmin, but not here - some fields come empty or          messed        up????
if (isset($title) && isset($keywords) && isset($description) && isset($text) && isset($page_id)) {


                            $query = mysql_query("UPDATE pages SET title = '$title', description = '$description', keywords = '$keywords', text = '$text'  WHERE page_id = '$page_id' ");




                            if ($query == TRUE) {

                                echo "<p>Page Updated</p>";
                                echo "<p><a href = 'http://localhost:8888/travel.ru/admin/index.php'>
        Edit Another Page</a></p>";
                            } else {
                                echo "<p>Page Is Not Updataed</p>";
                            }
                        } else {

                            echo "<p>You Left Some Fields Empty. Page Will Not Be Updated.</p>";
                        }
                        ?>

这是我使用的形式:

<form name="update" action = "update.php" method= "post">

<p> Page Name<br>
    <input value = "<?php echo $res['title']; ?>" type = "text" name = "title"></p>


<p> Page Description<br>
    <input value = "<?php echo $res['description']; ?>" type = "text" name = "title"></p>


<p> Page Keywords<br>
    <input value = "<?php echo $res['keywords']; ?>" type = "text" name = "title"></p>


<p> Page Content<br>
    <textarea type = "text" name ="text" cols = "68" rows = "15"><?php echo $res['text']; ?>
    </textarea></p>

<input type = "hidden" name="page_id" value =$res[page_id]>

<p><input type = "submit" name ="submit" value ="Save Changes" id="submit"</p>


</form>

任何帮助都会受到最高的赞赏,因为我不知道为什么会遇到这个问题?

2 个答案:

答案 0 :(得分:5)

您的大多数表单字段都命名为title。因此,您实际上没有名为descriptionpage_idkeywords的字段。

Mate也提出了一个有效的观点。

答案 1 :(得分:4)

尝试在输入值中添加php标记

 <input type = "hidden" name="page_id" value ="<?php echo $res['page_id']; ?>"  />

如前所述Amadan,还要检查表单中所有控件的名称。