2012-02-29 50 views
0

我有點迷路了數據庫的新手。我試圖創建一個頁面/表單,從一個只有管理員才能看到的鏈接中,從數據庫中獲取$ id的信息,顯示它以便每個字段都可以更新並保存回數據庫。我還在頁面底部添加了一個表單,以便將新字段輸入到數據庫中。爲了簡單起見,我將它們放在一頁上,但如果需要,我可以將它們分開。我的代碼還沒有工作,任何幫助將不勝感激。MySQL的PHP​​插入和更新

echo "<a href=\"test.html?p=ID{$row["id"]}\">Edit</a><br>"; 

吐出連結的test.html P = ID(#) 連桿部分被正常使用。

管理頁面

<div><h1 class="pageTitle">Episode List UPDATE</h1></div> 
    <?php 
// Connect to the database 
require 'include/episodelist.db.php'; 

// Ask the database for the information from the season table BUT how to only get info for the ID? 
$query="SELECT * FROM `season` WHERE `id` = $id"; 
$result = mysql_query("SELECT * FROM season"); 
$num=mysql_numrows($result); 
mysql_close(); 
?> 
<form action="include/epslist.update.php" method="POST"> 
<table> 
<tr> 
<td>Season Number: </td><td><input type="text" name="season_sum" size="50" value="<? echo "$season_num";?>"></td> 
</tr> 
<tr> 
<td>Episode Number: </td><td><input type="text" name="eps_num" size="50" value="<? echo "$eps_num";?>"></td> 
</tr> 
<tr> 
<td>Temp Episode Number: </td><td><input type="text" name="temp_eps_num" size="50" value="<? echo "$temp_eps_num";?>"></td> 
</tr> 
<tr> 
<td>Title: </td><td><input type="text" name="title" size="50" value="<? echo "$title";?>"></td> 
</tr> 
<tr> 
<td>Description: </td><td><textarea type="text" name="descrip" cols="50" rows="7" value="<? echo "$descrip";?>"></textarea></td> 
</tr> 
<tr> 
<td colspan="2"> 
<input type="Submit" value="Update"> 
</td> 
</tr> 
</table></form> 
<div class="Down10px"></div> 
<div><h1 class="pageTitle">Episode List NEW</h1></div> 
<form action="include/epslist.new.php" method="POST"> 
<table> 
<tr> 
<td>Season Number: </td><td><input type="text" name="season_sum" size="50" value="<? echo "$season_num";?>"></td> 
</tr> 
<tr> 
<td>Episode Number: </td><td><input type="text" name="eps_num" size="50" value="<? echo "$eps_num";?>"></td> 
</tr> 
<tr> 
<td>Temp Episode Number: </td><td><input type="text" name="temp_eps_num" size="50"></td> 
</tr> 
<tr> 
<td>Title: </td><td><input type="text" name="title" size="50"></td> 
</tr> 
<tr> 
<td>Description: </td><td><textarea type="text" name="descrip" cols="50" rows="7"></textarea></td> 
</tr> 
<tr> 
<td colspan="2"> 
<input type="Submit" value="New Item"> 
</td> 
</tr> 
</table></form> 

epslist.update.php

<?php 
require "episodelist.db.php"; 

//Test for user inout 
if (!empty($_GET[season_sum])&& 
    !empty($_GET[eps_num])&& 
    !empty($_GET[temp_eps_num])&& 
    !empty($_GET[title])&& 
    !empty($_GET[descrip])) 

    { 
// Im at a lost how to pull in the data just for test.html&=edit(#), check if a user changed it, then if so update that value in the database 

    // Trying to redirect the user back to the list once the update is finished 
     header("Location:"); 
?> 

epslist.new.php 修訂

<?php 
require "episodelist.db.php"; 

//Test for user inout 
if (!empty($_POST[season_sum])&& 
    !empty($_POST[eps_num])&& 
    !empty($_POST[temp_eps_num])&& 
    !empty($_POST[title])&& 
    !empty($_POST[descrip])) 

    { 
     $season_sum = mysqlclean($_POST,"season_sum",$link); 
     $eps_num = mysqlclean($_POST,"eps_num",$link); 
     $temp_eps_num = mysqlclean($_POST,"temp_eps_num",$link); 
     $title = mysqlclean($_POST,"title",$link); 
     //This is a text area cols 50 rows 7 
     $descrip = mysqlclean($_POST,"descrip",$link); 
     $ID = mysqlclean($_POST,"id",$link); 
    } 

    if (! empty($_POST['ID'])) 
    $id = (int)$_POST['ID']; 
    else $id = 'NULL'; 

    //Insert new entry 
    $query = "INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '{$descrip}')"; 

    // Trying to rederect the user back to the list once the update is successfully finished 
     header("Location: episodelist_superadmin.html"); 
?> 
+0

請提出您的問題標題描述的問題,而不是僅僅列出技術。 – 2012-02-29 13:37:13

回答

2

我不太明白你的問題,但我想你要求的是對

的一些修改
$query="SELECT * FROM `season` WHERE 1"; 

這應該是這樣的

$query="SELECT * FROM `season` WHERE `id` = $id"; 
+0

生病給了一個鏡頭,將當前的信息拉入表單是問題的第一部分。讓我們看看是否解決了第一個問題:) – 2012-02-29 13:14:37

+0

好吧,我想我的第一個問題是將鏈接中的id值傳遞給表單頁面。所以'admin.html&= ID {$ row ['id']} \「>編輯
」;'去admin.html&= ID1。在表單頁面上,我需要將匹配ID1的數據庫信息拉出URL字符串。除非用另一種方法根據用戶從管理頁面上的列表中單擊的鏈接來告訴表單需要提取哪些數據庫信息? – 2012-02-29 13:27:10

+0

您需要閱讀有關HTML表單以及使用GET和POST。我感覺很好,所以我會指出你。你需要你的鏈接看起來像admin.html?ID = $ row ['id'],然後你可以在查詢中做$ _GET ['ID']。 – Josh 2012-02-29 13:30:55

0

我不能正確理解你的問題,你想顯示一個鏈接,當鏈接被點擊的信息的新頁面顯示與此相關的鏈接編號,並在編輯它後,你想保存在數據庫中對此ID?

如果你想要這個答覆,那麼我會給你一個代碼來解決你的問題。

+0

該鏈接顯示在從我的數據庫中提取的信息旁邊。當用戶點擊(示例)項目4的鏈接時,他會被帶到一個表單頁面,其中所有項目4的信息都填充在正確的表單域中。然後,他更新一些(例如)價格。點擊更新,然後被踢回到所有項目列表中,它顯示了項目4的新價格。 – 2012-02-29 13:41:10

0

一兩件事我注意到...

$query="SELECT * FROM `season` WHERE 1"; 
$result = mysql_query("SELECT * FROM season"); 
$num=mysql_numrows($result); 

你沒有真正使用$查詢變量的代碼(管理頁面)的任何地方。假設你設置爲喬希表示$查詢,你會希望它看起來是這樣的:

$id = mysql_real_escape_string($id); 
$query="SELECT * FROM `season` WHERE `id` = $id"; 
$result = mysql_query($query); 
$num=mysql_numrows($result); 
+0

對,我的問題是,從URL字符串中提取ID,然後進入數據庫並填充所有表單域基於該ID的正確內容。 (例子)如果我去了admin.html?p = ID4,那麼表單應該充滿來自ID4下的數據庫的所有信息。這樣用戶可以更新信息並將其提交回數據庫。 – 2012-02-29 13:45:21

1

除了別人都已經提到了,你把你的數據的方法=「POST」,但您的更新頁面只有當前值爲GET時才運行。首先將$ _GET替換爲$ _POST。

代碼中缺少很多東西。我誠實的建議是閱讀更多內容或通過嘗試更簡單的任務開始。在這一點上,如果我們輸入PHP的樣子,你會得到更多由我們製作的東西。

編輯:

一個非常巧妙的解決辦法,你所能做的就是重用epslist.new.php的更新:

if (! empty($_POST['ID'])) 
    $id = (int)$_POST['ID']; 
else $id = 'NULL'; 

$query = "INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '{$descrip}') ON DUPLICATE KEY UPDATE season_num = VALUES(season_num), temp_eps_num = VALUES(temp_eps_num), esp_num = VALUES(esp_num), title = VALUES(title), descrip = VALUES(descrip)"; 

,並添加一個隱藏的輸入,它發送,如果你有ID一個(更新頁面)。

在這裏閱讀更多ON DUPLICATE KEY UPDATE

2日編輯:

現在我們潛水深入到MySQL的位置:

$query = " 
INSERT INTO season (ID, season_num, temp_eps_num, esp_num, title, descrip) 
SELECT * 
FROM (SELECT {$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '{$descrip}') AS h 
HAVING (SELECT COUNT(*) FROM season WHERE ID = {$id}) OR {$id} IS NULL 
ON DUPLICATE KEY UPDATE season_num = VALUES(season_num), temp_eps_num = VALUES(temp_eps_num), esp_num = VALUES(esp_num), title = VALUES(title), descrip = VALUES(descrip) 
"; 

認爲它的工作原理...:P對不起可能會讓你感到困惑。

+0

我希望自己沒有我想象的那麼遠。它聽起來像在我的腦海中,就像一個簡單的任務:) – 2012-02-29 13:47:36

+0

但是我已經從$ _GET改爲$ _POST – 2012-02-29 13:49:53

+0

但是我的new.php代碼仍然不想運行... – 2012-02-29 13:53:12