에이바바
에이바바

Reputation: 1031

MySQL update two different tables at once (CRUD)

I have two tables like this:

[tblFacilityHrs] id uid title description

[tblFacilityHrsDateTimes] owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate

I've joined the table as follows:

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

To display both tables I go through each record and put it all in a table:

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>";   

My question is now, how do I edit each record?

In my main page under the table I would normally do this:

pseudo code

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> "; 

Then on my edit page I would do something like this:

pseudo code

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>"; 

How does this work since the tables are joined? How can I edit both of them?

Upvotes: 1

Views: 1024

Answers (1)

gwt
gwt

Reputation: 2413

I think you should update each Table seperatly ; first the master table and then the detail table , and after that reabind your dataview

Upvotes: 3

Related Questions