2012-12-07 54 views
0

我想構建由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> 

任何幫助將最受讚賞,因爲我沒有線索爲什麼我有這個問題?

回答

5

大多數表單字段被命名爲title。因此,你實際上並沒有一個叫descriptionpage_idkeywords場。

伴侶也提出了一個正確的觀點。

+0

太謝謝你了!現在它工作了!不敢相信我錯過了!) – user1838334

4

嘗試加入PHP的標籤,就可以輸入值

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

如前所述Amadan,還要檢查名稱爲表單的所有控件。

+0

是的,這是對我錯過了的事!再加上表單名稱字段被搞砸了!謝謝你太多了 – user1838334