MySQL一次更新两个不同的表(CRUD)

时间:2012-05-07 15:13:20

标签: php mysql join crud

我有两张这样的表:

[tblFacilityHrs] id uid title description

[tblFacilityHrsDateTimes] owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate

我已加入表格如下:

$result = mysql_query("SELECT * FROM tblFacilityHrs JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid =tblFacilityHrsDateTimes.owner_uid") or trigger_error(mysql_error());

要显示两个表格,我会浏览每个记录并将其全部放在表格中:

while($row = mysql_fetch_array($result)){ 
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
echo "<tr>";  
echo "<td valign='top'>" . nl2br( $row['title']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['description']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['startEventDate']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['endEventDate']) . "</td>"; 
echo "<td valign='top'>" . nl2br( $row['startTime']) . "</td>";     
echo "<td valign='top'>" . nl2br( $row['endTime']) . "</td>"; 
echo "<td valign='top'>" . nl2br( $row['days']) . "</td>";     
echo "<td valign='top'>" . nl2br( $row['recurrence']) . "</td>";   
echo "<td valign='top'>" . nl2br( $row['finalDate']) . "</td>";   

我现在的问题是,如何编辑每条记录?

在我的主页下面,我通常会这样做:

伪代码

echo "<td valign='top'><a href=edit.php?id={$row['id']}>Edit</a></td><td><a href=delete.php?id={$row['id']}>Delete</a></td> "; 

然后在我的编辑页面上我会做这样的事情:

伪代码

if (isset($_GET['id']) ) { 
    $id = (int) $_GET['id']; 
    if (isset($_POST['submitted'])) { 
    foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
    $sql = "`title` =  '{$_POST['title']}' ,  `description` =  '{$_POST['description']}'   WHERE `id` = '$id' "; 
    mysql_query($sql) or die(mysql_error()); 
    echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />"; 
    echo "<a href='list.php'>Back</a>"; 

自表格加入以来,这是如何工作的?我怎样才能编辑它们?

1 个答案:

答案 0 :(得分:3)

我认为你应该分别更新每个表格;首先是主表,然后是详细信息表,然后重新绑定数据视图