2017-06-15 96 views
0

我有一個頁面從數據庫中檢索培訓廣告列表並顯示在表格中。另一個頁面顯示訓練名稱時點擊訓練的詳細信息。詳細信息頁面還允許編輯和刪除廣告。當廣告被編輯或刪除時,頁面被重定向到廣告列表。問題在於,如果我編輯廣告,並且在重定向到廣告列表頁面時刪除廣告,則不會訪問其他頁面,但刪除的廣告仍在列表中。但是,如果我刪除廣告而無需進行編輯,或者如果我訪問了另一個頁面,則可以在編輯完成後返回並刪除廣告,一切正常。對不起,很長的問題。任何形式的幫助表示讚賞。 代碼如下。如果第一個提交的頁面上的第二個PHP頁面沒有刷新目標頁面

廣告列表頁:

$connect = mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect database"); 
$sql = "SELECT * FROM ad WHERE trainer_id=$trainer_id ORDER BY id desc "; 
$result = mysqli_query($connect,$sql) or die("Couldn't execute query."); 
$count=mysqli_num_rows($result); 
       $totalPages=ceil($count/$result_per_page); 
$sql = "SELECT * FROM ad WHERE trainer_id=$trainer_id ORDER BY id desc LIMIT $start, $result_per_page "; 
$result = mysqli_query($connect,$sql) or die("Couldn't execute query."); 
if($count!=0) 
{ 
echo "<table cols='4' cellspacing='25'>"; 

echo "<th>Training Name</th>"; 
echo "<th>Trainer/Training Center</th>"; 
echo "<th>Training Dates</th>"; 
echo "<th>Registration Starts</th>"; 
echo "<th>Viewed</th>"; 

while($row=mysqli_fetch_assoc($result)) 
{ 

extract($row); 



echo "<tr><td ><a href='personal_ad_details.php?id=$id'>$training_name</a></td>"; 
echo "<td >$trainer_name</td>"; 
echo "<td >$training_start - $training_end</td>"; 
echo "<td >$reg_start</td>"; 
echo "<td >$view</td></tr>"; 

} 

echo "</table>"; 

廣告詳情頁(編輯和刪除):

$connect = mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect database"); 
if(isset($_GET['id']) && !empty($_GET['id'])){ 

$currentID = $_GET['id']; 
$sql = "SELECT * FROM ad WHERE id=$currentID"; 
$result = mysqli_query($connect,$sql) or die("Couldn't execute query."); 
$row= mysqli_fetch_array($result); 
extract($row); 
echo "<div id='ad_header'>"; 
echo "<h2> 
$training_name</h2>"; 
echo "<p>Trainer/Training Center:</p> $trainer_name "; 
echo "<p>Training Date(s): </p>$training_start - $training_end"; 
echo "<p>Registration starts: </p>$reg_start"; 
echo "<p>Phone: </p>$mobile"; 
if($email!=''){echo "<p>Email: </p><a href='mailto:$email'>$email</a>";} 
if($website!=''){echo "<p>Web Site: </p><a href='http://$website' target='_blank'>$website</a>";} 

echo "</br>"; 

echo "</div>"; 
echo "<div id='ad_main'>"; 

echo "<p>Training details:</p> </br>"?> <form action="<?php echo htmlspecialchars('personal_ad_details.php')?><?php echo "?id=".$currentID?>" method="post"> <textarea style="position:relative; left:0%" rows="10" cols="25" name="Telim_Melumatlari" ><?php echo $training_details; ?></textarea> 
<?php 
echo "</div>"; 

} 
?> 
<input style="margin:10px" type="submit" name="update" value="UPDATE"></form> 

<?php 
if(isset($_POST['update'])) 
{ 

    include('misc.inc'); 


    $connect = mysqli_connect($host,$user,$password,$database) 
        or die ("Couldn't connect to database."); 
    $sql  = "UPDATE ad SET training_details='$_POST[Telim_Melumatlari]' WHERE id=$currentID "; 
    mysqli_query($connect,$sql) or die("Couldn't execute query"); 
    $url="personal_ad_list.php"; 
    echo '<script type="text/javascript">'; 
      echo 'window.location.href="'.$url.'";'; 
      echo '</script>'; 
      echo '<noscript>'; 
      echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
      echo '</noscript>'; exit; 

} 

?> 
<form action="<?php echo htmlspecialchars('personal_ad_details.php')?><?php echo "?id=".$currentID?>" onSubmit="return confirm('Are you sure you want to delete the Ad?');" method="post"><input style="margin:10px; background-color:red" type="submit" name="delete" value="DELETE AD"></form> 
<?php 
if(isset($_POST['delete'])) 
{ 

    include('misc.inc'); 


    $connect = mysqli_connect($host,$user,$password,$database) 
        or die ("Couldn't connect to database."); 
    $sql  = "DELETE FROM ad WHERE id=$currentID "; 
    mysqli_query($connect,$sql) or die("Couldn't execute query"); 
    $url="personal_ad_list.php"; 
    echo '<script type="text/javascript">'; 
      echo 'window.location.href="'.$url.'";'; 
      echo '</script>'; 
      echo '<noscript>'; 
      echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
      echo '</noscript>'; exit; 

} 

?> 
+0

@DincaAdrian也許你有關於解決方案:) –

回答

0

您應該檢查你的表單操作的網址:

<form action="<?php echo htmlspecialchars('personal_ad_details.php')?><?php echo "?id=".$currentID?>" onSubmit="return confirm('Are you sure you want to delete the Ad?');" method="post"> 

<form action="" onSubmit="return confirm('Are you sure you want to delete the Ad?');" method="post"> 

注:還不錯的做法是,你應該提交頁面的過程中開始的代碼:

<?php 
$connect = mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect database"); 


if(isset($_POST['delete'])) 
{ 

    include('misc.inc'); 


    $connect = mysqli_connect($host,$user,$password,$database) 
        or die ("Couldn't connect to database."); 
    $sql  = "DELETE FROM ad WHERE id=$currentID "; 
    mysqli_query($connect,$sql) or die("Couldn't execute query"); 
    $url="personal_ad_list.php"; 
    echo '<script type="text/javascript">'; 
      echo 'window.location.href="'.$url.'";'; 
      echo '</script>'; 
      echo '<noscript>'; 
      echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
      echo '</noscript>'; exit; 

} 

if(isset($_POST['update'])) 
{ 

    include('misc.inc'); 


    $connect = mysqli_connect($host,$user,$password,$database) 
        or die ("Couldn't connect to database."); 
    $sql  = "UPDATE ad SET training_details='$_POST[Telim_Melumatlari]' WHERE id=$currentID "; 
    mysqli_query($connect,$sql) or die("Couldn't execute query"); 
    $url="personal_ad_list.php"; 
    echo '<script type="text/javascript">'; 
      echo 'window.location.href="'.$url.'";'; 
      echo '</script>'; 
      echo '<noscript>'; 
      echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
      echo '</noscript>'; exit; 

} 


if(isset($_GET['id']) && !empty($_GET['id'])){ 

$currentID = $_GET['id']; 
$sql = "SELECT * FROM ad WHERE id=$currentID"; 
$result = mysqli_query($connect,$sql) or die("Couldn't execute query."); 
$row= mysqli_fetch_array($result); 
extract($row); 
echo "<div id='ad_header'>"; 
echo "<h2> 
$training_name</h2>"; 
echo "<p>Trainer/Training Center:</p> $trainer_name "; 
echo "<p>Training Date(s): </p>$training_start - $training_end"; 
echo "<p>Registration starts: </p>$reg_start"; 
echo "<p>Phone: </p>$mobile"; 
if($email!=''){echo "<p>Email: </p><a href='mailto:$email'>$email</a>";} 
if($website!=''){echo "<p>Web Site: </p><a href='http://$website' target='_blank'>$website</a>";} 

echo "</br>"; 

echo "</div>"; 
echo "<div id='ad_main'>"; 

echo "<p>Training details:</p> </br>"?> <form action="" method="post"> <textarea style="position:relative; left:0%" rows="10" cols="25" name="Telim_Melumatlari" ><?php echo $training_details; ?></textarea> 
<?php 
echo "</div>"; 

} 

?> 
<input style="margin:10px" type="submit" name="update" value="UPDATE"></form> 

<form action="" onSubmit="return confirm('Are you sure you want to delete the Ad?');" method="post"><input style="margin:10px; background-color:red" type="submit" name="delete" value="DELETE AD"></form> 
+0

感謝你的努力的想法,但他們都不爲我工作。當我更新信息並立即刪除廣告時,培訓列表不刷新 –

+0

您的意思是刷新。您正在重定向該頁面。如果不工作,那麼使用該位置,並檢查數據庫您的查詢工作是否正常 –

+0

是的,它重定向到廣告列表頁面。查詢也可以正常工作,因爲重定向到廣告列表頁面後,我訪問另一個頁面並返回到廣告列表頁面,刪除的廣告不再出現在列表中 –