2012-11-28 19 views
1

我想創建一個背板到我的電視項目。我的MySql數據庫由兩個表格組成。 「顯示」&「季節」。更新記錄電視節目數據庫 - PHP的MYSQL

The Shows表由顯示ID(主鍵),顯示標題&日期組成。季節表由季節ID(主鍵),顯示ID(外鍵),季節,發佈日期組成。

您在控制面板中看到的第一件事是以表格格式從數據庫中顯示的所有節目。例如,如果您編輯「德克斯特」,它會帶您進入編輯頁面,顯示德克斯特季節的輸入框。像第1季,第2季等...我有這種工作。我的意思是它在輸入框中正確顯示,但是當我去提交它只是重定向回到相同的編輯頁面,沒有更新。

從我的index.php背板的代碼,允許你編輯:從edit.php

<td><a href="edit.php?id=<?php echo $row['show_id']; ?>">edit</a></td> 

代碼:

<?php 
$playlist_id=$_GET['id']; 
// this makes it so it grabs the season records that match the show id 
$sql="SELECT * FROM seasons WHERE show_id='$show_id'"; 
$result=mysql_query($sql); 
$count=mysql_num_rows($result); 
?> 



<table> 
<form name="form1" method="post" action=""> 
<tr> 
<td> 
<table> 
<tr> 
<td align="center"><strong>Id</strong></td> 
<td align="center"><strong>Season Name</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 

<tr> 
<td align="center"> 
<input name="season_id[]" type="text" id="season_id" value="<?php echo $rows['season_id']; ?>"> 
</td> 
<td align="center"> 
<input name="Season[]" type="text" id="Season" value="<?php echo $rows['Season']; ?>"> 
</td> 
</tr> 

<?php 
} 
?> 

<tr> 
<td></td> 
<td></td> 
<td colspan="4" align="right"><input type="Submit" name="Submit" value="Submit"></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 

<?php 

$result1 = FALSE; 
if (isset($_GET['Submit'])) { 
for($i=0;$i<$count;$i++){ 
$sql1="UPDATE seasons SET Season='$Season[$i] WHERE Season_id='$Season_id[$i]'"; 
$result1=mysql_query($sql1); 
} 
} 

if($result1){ 
header("location:update.php"); 
} 
mysql_close(); 
?> 

你可能想知道爲什麼我想編輯的季節的名字,但我最終會讓它變得更加複雜,一旦我越過這個障礙,就會增加更多的場地。就像我說的,它顯示正常,但當我點擊提交它只是重新加載頁面,沒有任何變化。

回答

0

您發佈的數據,所以你需要改變這個isset($ _ GET [ '提交'])來isset($ _ POST [ '提交'])

您在更新查詢錯誤。在$季節後失蹤'[$ i]

$ sql1 =「UPDATE seasons SET Season ='$ Season [$ i]'WHERE Season_id ='$ Season_id [$ i]'」;

您可以使用mysql_affected_rows()來檢查行是否已更新。

if(mysql_affected_rows() == 1){ 
echo "Updated"; 
} else { echo "error"; } 
+0

已更改,但它仍在刷新頁面並且沒有任何內容正在提交到數據庫。 –

+0

我編輯答案 –

+0

它仍然刷新頁面。如果我想讓它做更新在另一個頁面上的sql會更容易,如果是的話,我將如何把它放到不同的頁面上。我知道我可以做的

但是然後我會把procupdate.php –

0

您希望在頁面張貼時執行查詢。您edit.php頁應該是這樣的:

<?php 
if (isset($_POST['submit'])) { 
    $sql = "UPDATE `table_name` (`column1`, `column2`) VALUES (:value1, :value2)"; 
    $statement = $pdo->prepare($sql); 
    $statement->bindParam(':value1', $_POST['some_field_name']); 
    $statement->bindParam(':value1', $_POST['some_field_name']); 
    $statement->execute(); 
    // record's updated; either redirect or display error message 
} 
?> 
<!-- HTML of form here; will be shown if nothing’s been POSTed --> 

其中,「提交」是您的提交按鈕的名稱,即<input type="submit" name="submit" value="Update" />

您還應該使用類似PDO而不是mysql_函數,因爲現在不推薦使用這些函數。