我有兩個表是這樣的:MySQL的一次更新兩個不同的表(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>";
這些表格在加入後如何工作?我如何編輯它們兩個?
我不知道它是如何工作的,因爲你賦給'$ sql'的值不是一個有效的查詢。錯誤的複製粘貼? – Romain
如果我只有一張桌子,那麼最後兩個代碼塊就不會工作。 –
好,你擔心'mysql_real_escape',但如果你需要佔位符(即「我應該把什麼我要求」),然後在片段中適當地標記它們。目前這令人困惑。 – Romain