更新唱片电视节目数据库 - PHP MYSQL

时间:2012-11-28 15:02:40

标签: php mysql

我正在尝试为我的电视项目创建一个后面板。我的MySql数据库由两个表组成。 “显示”& “四季”。

“显示”表包含“显示ID(主键)”,“显示标题”和“显示标题”。日期。四季表包括季节ID(主键),显示ID(外键),季节,发布日期。

您在控制面板中看到的第一件事是表格格式的数据库中的所有节目。例如,如果您编辑“Dexter”,它将带您进入编辑页面,显示dexter季节的输入框。像第1季,第2季等......我有这种工作方式。我的意思是它在输入框中正确显示但是当我去提交它时只是重定向回到相同的编辑页面而没有任何更新。

我的主index.php背景中的代码,允许您编辑:

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

edit.php中的代码:

<?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();
?>

你可能想知道我为什么要编辑季节的名字,但是一旦我越过这个障碍,我最终会让它更复杂并添加更多的字段。就像我说它显示正确,但当我点击提交它只是重新加载页面,没有任何变化。

2 个答案:

答案 0 :(得分:0)

您正在发布数据,因此您需要将此isset($ _ GET ['Submit'])更改为isset($ _ POST ['Submit'])

更新查询中有错误。在$ Season [$ 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"; }

答案 1 :(得分: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_函数,因为这些函数现已弃用。