這是我想要做的,我有兩個頁面,第一個是「edit.php」,第二個是「edit2.php」。頁面「edit.php」這是顯示所有新聞的頁面,您將通過點擊「編輯按鈕」和「edit2.php」來選擇要編輯的新聞,我將在哪裏編輯新聞。我想在另一個頁面中傳遞所選新聞的價值。但問題在於,當我點擊「Sample news 1」編輯按鈕時,顯示在另一個頁面中的數據是「Sample news 2」。即使當我點擊「Sample news 2」編輯按鈕時,數據也是「Sample news 2」。有人可以給我如何解決這個問題的想法嗎?PHP - 錯誤的數據通過並顯示到另一頁
這裏是edit.php的圖片。我點擊「Sample news 1」的編輯按鈕。
這裏是edit2.php的圖片。這是輸出數據。數據應該是「Sample news 1」而不是「Samplel news 2」。
這裏是edit.php我的PHP代碼
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $newsid;
$_SESSION['n_title'] = $title;
$_SESSION['n_date'] = $date;
$_SESSION['n_content'] = $content;
$_SESSION['n_image'] = $newsimage;
header('Location: edit2.php');
}
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
這裏是 「edit2.php」 我的PHP代碼
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $_SESSION['n_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $_SESSION['n_date']; ?>" /><br>
Content<textarea name="content"><?php echo $_SESSION['n_content']; ?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $_SESSION['n_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
你好,先生,對不起,對於遲到的回覆,但它已經通過@zan的回答修復了。對於你的努力 – nethken
你好,先生?@Rajeep – nethken
@nethken你是否用這些代碼片段測試了你的應用程序? –