如果有耐心的人可以閱讀本文並幫助我,我會非常感激。我無法更新一個MySQL表。我有一張表,用於存儲dj對唱片專輯的評論和反饋,並且我還有第二張表,用於存儲有關專輯的一般信息。我的問題是通過每個反饋行循環更新我的表。我只是要發佈我的所有代碼,也許它會更清晰(對於冗長的文章感到抱歉)。Mysql PHP更新結果列表中的表格
<?php
$done = false;
$problem = false;
$expected = array('album_id', 'dj','affilliations','rating','comments'
, 'content_id','title','ep','date','genre');
$conn = dbConnect('admin');
if ($_GET && !$_POST) {
if (isset($_GET['album_id']) && is_numeric($_GET['album_id'])) {
$album_id = $_GET['album_id'];
}
else {
$album_id = NULL;
}
if ($album_id) {
//this statement updates album_info correctly, but not album_comments
$sql = "SELECT album_info.album_id, album_info.title, album_info.ep
, album_info.genre, album_info.date, album_comments.content_id
, album_comments.album_id, album_comments.dj
, album_comments.affilliations, album_comments.rating
, album_comments.comments
FROM album_info, album_comments
WHERE album_info.album_id = $album_id
AND album_comments.album_id = $album_id";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($result);
}
}
// if form has been submitted, update record
if (array_key_exists('update', $_POST)) {
// prepare expected items for insertion in to database
foreach ($_POST as $key => $value) {
if (in_array($key, $expected)) {
${$key} = mysql_real_escape_string($value);
}
}
// abandon the process if primary key invalid
if (!is_numeric($album_id)) {
die('Invalid request');
}
if(!empty($_POST['dj']) && !empty($_POST['title'])) {
$album_id = mysql_real_escape_string(trim($album_id));
$dj = mysql_real_escape_string(trim($_POST['dj']));
$affilliations = mysql_real_escape_string(trim($_POST['affilliations']));
$rating = mysql_real_escape_string(trim($_POST['rating']));
$comments = mysql_real_escape_string(trim($_POST['comments']));
$title = mysql_real_escape_string(trim($_POST['title']));
$ep = mysql_real_escape_string(trim($_POST['ep']));
$genre = mysql_real_escape_string(trim($_POST['genre']));
$date = mysql_real_escape_string(trim($_POST['date']));
}
$sql="UPDATE album_info, album_comments
ON album_info.album_id = album_comments.album_id
SET album_info.title = '$title', album_info.ep = '$ep'
, album_info.date = '$date', album_info.genre = '$genre'
, album_comments.dj = '$dj'
, album_comments.affilliations = '$affilliations'
, album_comments.rating = '$rating'
, album_comments.comments = '$comments'
album_comments.album_id = '$album_id'
AND album_info.album_id = '$album_id'";
// submit the query and redirect if successful
$done = mysql_query($sql) or die(mysql_error());
if($done) {
printf("<script>location.href='?page=albums'</script>");
}
}
?>
這是正確更新album_info,但需要通過循環播放,如下圖所示album_comments:
<form id="album_form" name="album_form" method="post" action="">
<fieldset>
<legend>Album Info</legend>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title"
value="<?php echo htmlentities($row['title']); ?>" />
</p>
<p>
<label for="ep">EP</label>
<input type="text" name="ep" id="ep"
value="<?php echo htmlentities($row['ep']); ?>" />
</p>
<p>
<label for="day">Date:</label>
<input name="day" type="text id="day: size="2" maxlength="2"
value="<?php echo htmlentities($row['date']); ?>"/>
</p>
<p>
<label for="genre">Genre</label>
<input type="text" name="genre" id="genre"
value="<?php echo htmlentities($row['genre']); ?>"/>
</fieldset>
</p>
<fieldset>
<legend>Comments</legend>
<!--data below is from table album_comments -->
<table id="tblInsertRowPHP" class="tableResults" cellpadding="0"
cellspacing="0">
<tbody>
<?php
//this spits out all the feedback for the particular album;
//this is the part I need help with
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<?php
echo '<input type="text" name="dj" size="15" value="'.$row['dj'].'" />';
echo '<input type="text" name="affilliations" size="30"
value="'.$row['affilliations'].'" />';
echo '<input type="text" name="rating" size="8"
value="'.$row['rating'].'" />';
echo '<input type="text" name="comments" size="68"
value="'.$row['comments'].'" />';
?>
</td>
</tr>
<?php }
$sql = "SELECT album_id FROM album_info";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($result);?>
</tbody>
</table>
</fieldset>
<input type="submit" name="update"
value="Update entry" id="submit" />
<input name="album_id" type="hidden"
value="<?php echo $row['album_id']; ?>" />
</form>
<?php } ?>
如何修改MYSQL語句要經過各行album_comments和更新呢?我是否需要準備好的聲明,還是可以更改PHP sql語句?再次感謝您的幫助 - 我在這方面有點新鮮。
$ SQL =「更新album_info,album_comments ON album_info.album_id = album_comments.album_id SET album_info.title ='$ title',album_info.ep ='$ ep' ,album_info.date ='$ date',albu m_info.genre = '$流派' ,album_comments.dj = '$ DJ' ,album_comments.affilliations = '$附屬機構' ,album_comments.rating = '$評級' ,album_comments.comments = '$意見' '這裏錯過了'album_comments.album_id ='$ album_id' AND album_info.album_id ='$ album_id'「; – Johan 2011-04-17 13:49:07
您在更新聲明中忘記了WHERE,請參閱上面的註釋。 – Johan 2011-04-17 13:49:31