因此,在我的网站上,我遇到了一个问题,即当我不想要它时,我的$ _SESSION变量被覆盖了。基本上我的网站如何运作:
问题是,当用户进行新审核时,之前的所有评论都与新评论具有相同的信息,因为$ _SESSION变量已更改。如何创建,以便在创建新评论时,先前评论中显示的信息不会更改(并且仍会显示)。 步骤1-6的代码(不包括建立与mysql数据库的连接):
<p>Please leave blank any entries below that you don't know</p>
<p>Artist:</p>
<input type="text" name="artistbox">
<p>Featured Artists:</p>
<input type="text" name="featuresbox">
<p>Title:</p>
<input type="text" name="songbox">
<p>Album:</p>
<input type="text" name="albumbox">
<p>Choose a genre:</p>
<select name="genre">
<option value="Pop">Pop</option>
<option value="Hip-hop">Hip-hop</option>
<option value="RnB">RnB</option>
<option value="EDM">Electronic Dance Music</option>
<option value="Jazz">Jazz</option>
<option value="Blues">Blues</option>
<option value="Rock">Rock</option>
<option value="Punk-rock">Punk-rock</option>
<option value="Reggae">Reggae</option>
<option value="Classic">Classic</option>
<option value="Funk">Funk</option>
<option value="Soul">Soul</option>
</select>
<p>Rating:</p>
<select name="rating">
<option value="0.5">0.5</option>
<option value="1.0">1.0</option>
<option value="1.5">1.5</option>
<option value="2.0">2.0</option>
<option value="2.5">2.5</option>
<option value="3.0">3.0</option>
<option value="3.5">3.5</option>
<option value="4.0">4.0</option>
<option value="4.5">4.5</option>
<option value="5.0">5.0</option>
</select>
<p>Comments (1000 character limit):</p>
<textarea name="commentsbox" cols="60" rows="7"></textarea>
<br>
<input class="submitbutton" type="submit" value="Submit">
$artistbox = $_POST['artistbox'];
$featuresbox = $_POST['featuresbox'];
$songbox = $_POST['songbox'];
$albumbox = $_POST['albumbox'];
$genre = $_POST['genre'];
$rating = $_POST['rating'];
$commentsbox = $_POST['commentsbox'];
date_default_timezone_set('NZ');
$time = date('Y/m/d H:i:s');
$queryartist = "INSERT INTO `artist` (`ArtistsName`)
VALUES ('{$artistbox}')";
$queryfeatures = "INSERT INTO `features` (`Features`)
VALUES ('{$featuresbox}')";
$querysong = "INSERT INTO `SongName` (`SongName`)
VALUES ('{$songbox}')";
$queryalbum = "INSERT INTO `album` (`Album`)
VALUES ('{$albumbox}')";
$querygenre = "INSERT INTO `genre` (`Genre`)
VALUES ('{$genre}')";
$queryrating = "INSERT INTO `rating` (`Rating`)
VALUES ('{$rating}')";
$querycomments = "INSERT INTO `comments` (`Comments`)
VALUES ('{$commentsbox}')";
$querydate = "INSERT INTO `DateReviewed` (`DateReviewed`)
VALUES ('{$time}')";
$conn->query($queryartist);
$conn->query($queryfeatures);
$conn->query($queryalbum);
$conn->query($querygenre);
$conn->query($queryrating);
$conn->query($querycomments);
$conn->query($querydate);
if ($conn->query($querysong) === TRUE)
{
$last_id = $conn->insert_id;
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$querylink = "INSERT INTO `link` (`link`) VALUES ('{$link}')";
$conn->query($querylink);
$newpage = fopen("form_$last_id.php", "w+");
copy ('template.php', "form_$last_id.php");
header("location: form_$last_id.php");
session_start();
$_SESSION["artist"]=$artistbox;
$_SESSION["features"]=$featuresbox;
$_SESSION["album"]=$albumbox;
$_SESSION["genre"]=$genre;
$_SESSION["rating"]=$rating;
$_SESSION["comments"]=$commentsbox;
$_SESSION["songbox"]=$songbox;
$_SESSION["time"]=$time;
exit ();
步骤7的代码(从template.php文件中提取并非所有代码):
session_start();
$artist = $_SESSION["artist"];
$features = $_SESSION["features"];
$album = $_SESSION["album"];
$genre = $_SESSION["genre"];
$rating = $_SESSION["rating"];
$comments = $_SESSION["comments"];
$song = $_SESSION["songbox"];
$time = $_SESSION["time"];
<p>
<?php
echo '<b>', 'By: ', '</b>'. $artist . ' ft. ' . $features .
'</br>';
echo '<i>' . $album . '</br>', '</br>', '</i>';
echo '<b>', 'Genre: ', '</b>' . $genre . '</br>', '</br>';
echo '<b>', 'Rating: ', '</b>' . $rating . '/5', '</br>',
'</br>';
echo '<b>', 'Comments: ', '</b>', '</br>', '</br>' .
$comments;
?>
</p>
答案 0 :(得分:0)
根据会话变量中的主键组织每个评论,您最终会得到一个多维数组。
当用户编写新评论时,您已将其存储在数据库中。完成后,使用它的pk将评论添加到您的会话变量中。您还需要将歌曲的主键存储在链接表中。
session_start();
$querylink = "INSERT INTO `link` (`link`) VALUES ('{$link}')";
$conn->query($querylink);
$_SESSION['reviews'][$conn->insert_id]["artist"] = $artistbox;
$_SESSION['reviews'][$conn->insert_id]["features"] = $featuresbox;
$_SESSION['reviews'][$conn->insert_id]["album"] = $albumbox;
$_SESSION['reviews'][$conn->insert_id]["genre"] = $genre;
$_SESSION['reviews'][$conn->insert_id]["rating"] = $rating;
$_SESSION['reviews'][$conn->insert_id]["comments"] = $commentsbox;
$_SESSION['reviews'][$conn->insert_id]["songbox"] = $time;
考虑更改您的设计方法,并将歌曲的ID也存储在链接表中。当用户点击某首歌曲时,您可以使用song_id查询您的数据库并显示该歌曲的所有评论。
当用户为歌曲撰写评论时,您会将评论存储在数据库中,包括$ song_id,并将其添加到会话变量中。像下面的东西。
session_start();
$querylink = "INSERT INTO `link` (`link`,`song_id`) VALUES ('{$link}','{$song_id}')";
$conn->query($querylink);
$_SESSION['reviews'][$song_id][$conn->insert_id]["artist"] = $artistbox;
$_SESSION['reviews'][$song_id][$conn->insert_id]["features"] = $featuresbox;
$_SESSION['reviews'][$song_id][$conn->insert_id]["album"] = $albumbox;
$_SESSION['reviews'][$song_id][$conn->insert_id]["genre"] = $genre;
$_SESSION['reviews'][$song_id][$conn->insert_id]["rating"] = $rating;
$_SESSION['reviews'][$song_id][$conn->insert_id]["comments"] = $commentsbox;
$_SESSION['reviews'][$song_id][$conn->insert_id]["songbox"] = $songbox;