在数据库中将已删除的产品(0)还原为(1)

时间:2018-03-28 05:04:50

标签: php pdo

我有一个应该有效的代码,但它没有,我找不到解决方案。 在我的数据库中,我删除了列 如果产品被删除则为1

[Deleted]

如果产品被删除,它会进入Archives位置,当点击按钮在数据库内变为0时,应该成功恢复该位置

enter image description here

这是我尝试使用PHP:

   <?php
include_once "../konfiguracija.php";
include 'includes/head.php';
include 'includes/izbornik.php';
//Restore Product
if(isset($_GET['restore'])) {
 $id = sanitize($_GET['restore']);
 $restorirana=$veza->prepare("UPDATE products SET deleted = 0 WHERE id='$id';");
 $restorirana->execute();
 header('Location: Products.php');
}
?>
<?php
$format = new NumberFormatter("en_US",NumberFormatter::CURRENCY);
$p_result =$veza->prepare( "SELECT * FROM `products` WHERE `deleted`=1;");
$p_result ->execute();
?>
<h2 class="text-center">Products</h2>
<div class="clearfix"></div>
<hr>
<table class="table table-bordered table-condensed talbe-striped">
 <thead>
   <th>Restore</th>
   <th>Product</th>
   <th>Price</th>
   <th>Parent ~ Category</th>
   <th>Featured</th>
   <th>Sold</th>
 </thead>
 <tbody>
    <?php while ($product = $p_result->fetch(PDO::FETCH_ASSOC)):
      $childId = $product['categories'];
       $catSql = $veza->prepare("SELECT * FROM categories WHERE id='$childId';");
       $result= $catSql->execute();
       $child = $catSql->fetch(PDO::FETCH_ASSOC);
       $parentId = $child['parent'];
         $parentSql = $veza->prepare("SELECT * FROM categories WHERE id='$parentId';");
        $parentSql->execute();
         $parent =$parentSql->fetch(PDO::FETCH_ASSOC);
         $category = $parent['category'].'~'.$child['category'];
   ?>
    <tr>
     <td>
      <a href="products.php?restore=<?php echo $product['id']; ?>" class ="button"><i class="fas fa-undo"></i></a>
    </td>
    <td><?=$product['title'];?></td>
    <td><?php echo $format->format ($product['price']); ?></td>
    <td><?php echo $category;?></td>
    <td><a href="products.php?featured=<?=(($product['featured']== 0)?'1':'0');?>&id=<?=$product['id'];?>"
      class ="button tiny"><i class="fas fa-<?=(($product['featured']==1)?'minus':'plus');?> fa-2x " ></i>
    </a>&nbsp <?=(($product['featured']==1)?'Featured Product': '');?></td>
    <td>0</td>
 </tr>
<?php endwhile; ?>
 </tbody>
</table>
<?php include 'includes/podnozje.php';
include_once 'includes/scripts.php';
 ?>*

按钮&#34;恢复&#34;按下没有输出错误,页面重定向到products.php并且删除的数据库内部保持不变。

1 个答案:

答案 0 :(得分:1)

恢复链接指向错误的php文件。

<强>替换

products.php?restore=

。通过

archived.php?restore=

将标题location正确重命名为products.php并在重定向后添加exit;,以便在重定向调用后不会执行其他代码。

发件人:

 header('Location: Products.php');

 header('Location: products.php');exit;