I can’t get a variable from another script [closed]

I was making a food webstore with admin page where I manage to create add, show all products, delete products but I have HUGE problem with editing.

For my editing system I’m using 3 separate scripts: edit.php, all-products-admin.php and edit-products-admin.php;
In summary: I have a list of all products in all-products-admin.php script and each of them has 2 buttons delete and submit with hidden input with returns the product’s id. After pressing a button I wanted my id to be written in $id_to_edit variable and then using header to go to edit-products-admin.php page where then use it. And somehow my edit-products-admin.php can’t see this variable at all!
There are solutions I have alredy tried that didn’t work for me:

  1. Creating session variable and sesssion_start() -> edit-products-admin.php can’t see it at all

  2. Using peace of edit.php in my edit-products-admin.php code -> $id_to_edit didn’t have any value at all, but session variable worked. Unfortunately couldn’t change it or unset later

  3. Using header with variable in it -> I could see right id on top of the page but code couldn’t see it at all.

edit.php –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//edit.php
<?php
include("database.php");
if ('POST' === $_SERVER['REQUEST_METHOD'])
{
if (isset($_POST['edit'])){
//unset($_SESSION['id_to_edit']);
$id_to_edit = $_POST['edit-id'];
$_SESSION['id_to_edit']= $_POST['edit-id'];
header("Location: edit-product-admin.php");
}
}
?>
</code>
<code>//edit.php <?php include("database.php"); if ('POST' === $_SERVER['REQUEST_METHOD']) { if (isset($_POST['edit'])){ //unset($_SESSION['id_to_edit']); $id_to_edit = $_POST['edit-id']; $_SESSION['id_to_edit']= $_POST['edit-id']; header("Location: edit-product-admin.php"); } } ?> </code>
//edit.php

<?php
include("database.php");

if ('POST' === $_SERVER['REQUEST_METHOD'])
{
    if (isset($_POST['edit'])){

        //unset($_SESSION['id_to_edit']);
    
        $id_to_edit = $_POST['edit-id'];
        $_SESSION['id_to_edit']= $_POST['edit-id'];

        header("Location: edit-product-admin.php");
    }
    
}
  
?>

all-products-admin.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
session_start();
include("header.php");
include("database.php");
include("sidebar.php");
include("delete.php");
include("edit.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles-admin.css?v=<?php echo time(); ?>">
<link rel="stylesheet" href="styles.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="space" ></div>
<div class="centered-text">
<h2>Product items</h2>
</div>
<table class="table">
<thead>
<tr>
<th>id</th>
<th>Title</th>
<th>Price</th>
<th>Discount</th>
<th>Weight</th>
<th>Calories</th>
<th>In store</th>
<th>Bought</th>
<th>Type of product</th>
<th>Company</th>
<th>Country</th>
<th>Is vegan</th>
<th>Lactose free</th>
<th>Gluten free</th>
<th>Image</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
$sql = "SELECT * FROM products";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
while($resultCheck>0)
{
$row = mysqli_fetch_assoc($result);
?>
<tr>
<td><?=$row["id"]?></td>
<td><?=$row["title"]?></td>
<td><?=$row["price"]?></td>
<td><?=$row["discount"]?></td>
<td><?=$row["weight"]?></td>
<td><?=$row["kalories"]?></td>
<td><?=$row["number_in_shop"]?></td>
<td><?=$row["number_bought"]?></td>
<td><?=$row["type_of_product"]?></td>
<td><?=$row["company_name"]?></td>
<td><?=$row["origin_country"]?></td>
<td><?=$row["vegan"]?></td>
<td><?=$row["lactose_free"]?></td>
<td><?=$row["gluten_free"]?></td>
<td><img height='40px' width='40px' src='<?=$row["photo_link"]?>'></td>
<td>
<form action="edit.php" method="post">
<input type="hidden" name="edit-id" value="<?php echo $row["id"]; ?>">
<input class="btn-edit" type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete-id" value="<?php echo $row["id"]; ?>">
<input class="btn-delete" type="submit" name="delete" value="Delete">
</form>
</td>
<?php
$resultCheck--;
}
?>
</table>
</body>
</html>
</code>
<code><?php session_start(); include("header.php"); include("database.php"); include("sidebar.php"); include("delete.php"); include("edit.php"); ?> <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles-admin.css?v=<?php echo time(); ?>"> <link rel="stylesheet" href="styles.css?v=<?php echo time(); ?>"> </head> <body> <div class="space" ></div> <div class="centered-text"> <h2>Product items</h2> </div> <table class="table"> <thead> <tr> <th>id</th> <th>Title</th> <th>Price</th> <th>Discount</th> <th>Weight</th> <th>Calories</th> <th>In store</th> <th>Bought</th> <th>Type of product</th> <th>Company</th> <th>Country</th> <th>Is vegan</th> <th>Lactose free</th> <th>Gluten free</th> <th>Image</th> <th colspan="2">Action</th> </tr> </thead> <?php $sql = "SELECT * FROM products"; $result = mysqli_query($conn, $sql); $resultCheck = mysqli_num_rows($result); while($resultCheck>0) { $row = mysqli_fetch_assoc($result); ?> <tr> <td><?=$row["id"]?></td> <td><?=$row["title"]?></td> <td><?=$row["price"]?></td> <td><?=$row["discount"]?></td> <td><?=$row["weight"]?></td> <td><?=$row["kalories"]?></td> <td><?=$row["number_in_shop"]?></td> <td><?=$row["number_bought"]?></td> <td><?=$row["type_of_product"]?></td> <td><?=$row["company_name"]?></td> <td><?=$row["origin_country"]?></td> <td><?=$row["vegan"]?></td> <td><?=$row["lactose_free"]?></td> <td><?=$row["gluten_free"]?></td> <td><img height='40px' width='40px' src='<?=$row["photo_link"]?>'></td> <td> <form action="edit.php" method="post"> <input type="hidden" name="edit-id" value="<?php echo $row["id"]; ?>"> <input class="btn-edit" type="submit" name="edit" value="Edit"> </form> </td> <td> <form action="delete.php" method="post"> <input type="hidden" name="delete-id" value="<?php echo $row["id"]; ?>"> <input class="btn-delete" type="submit" name="delete" value="Delete"> </form> </td> <?php $resultCheck--; } ?> </table> </body> </html> </code>
<?php
session_start();
include("header.php");
include("database.php");
include("sidebar.php");
include("delete.php");
include("edit.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles-admin.css?v=<?php echo time(); ?>">
<link rel="stylesheet" href="styles.css?v=<?php echo time(); ?>">

</head>
<body>
    <div class="space" ></div>
  <div class="centered-text">
    <h2>Product items</h2>

  </div>
  <table class="table">
    <thead>
      <tr>
        <th>id</th>
        <th>Title</th>
        <th>Price</th>
        <th>Discount</th>
        <th>Weight</th>
        <th>Calories</th>
        <th>In store</th>
        <th>Bought</th>
        <th>Type of product</th>
        <th>Company</th>
        <th>Country</th>
        <th>Is vegan</th>
        <th>Lactose free</th>
        <th>Gluten free</th>
        <th>Image</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <?php
       $sql = "SELECT * FROM products";
       $result = mysqli_query($conn, $sql);
       $resultCheck = mysqli_num_rows($result);
   
      while($resultCheck>0)
       {
           $row = mysqli_fetch_assoc($result);
    ?>
    <tr>
      <td><?=$row["id"]?></td>
      <td><?=$row["title"]?></td>
      <td><?=$row["price"]?></td>      
      <td><?=$row["discount"]?></td> 
      <td><?=$row["weight"]?></td>    
      <td><?=$row["kalories"]?></td>
      <td><?=$row["number_in_shop"]?></td>    
      <td><?=$row["number_bought"]?></td>  
      <td><?=$row["type_of_product"]?></td>  
      <td><?=$row["company_name"]?></td>  
      <td><?=$row["origin_country"]?></td>  
      <td><?=$row["vegan"]?></td> 
      <td><?=$row["lactose_free"]?></td>   
      <td><?=$row["gluten_free"]?></td>  
      <td><img height='40px' width='40px' src='<?=$row["photo_link"]?>'></td>
       
      <td>
      <form action="edit.php" method="post">
      <input type="hidden" name="edit-id" value="<?php echo $row["id"]; ?>">
            <input class="btn-edit" type="submit" name="edit" value="Edit">
        </form>
      </td> 

      <td>
      <form action="delete.php" method="post">
      <input  type="hidden" name="delete-id" value="<?php echo $row["id"]; ?>">
          <input class="btn-delete" type="submit" name="delete" value="Delete">
        </form>
      </td> 

      <?php
           
            $resultCheck--;
        }
       
      ?>
  </table>

  

  
</body>
</html>

edit-product-admin.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
session_start();
include("header.php");
include("database.php");
include("sidebar.php");
include("delete.php");
include("edit.php");
echo "Id get : " ;
echo $_SESSION['id_to_edit'];
echo $id_to_edit;
</code>
<code><?php session_start(); include("header.php"); include("database.php"); include("sidebar.php"); include("delete.php"); include("edit.php"); echo "Id get : " ; echo $_SESSION['id_to_edit']; echo $id_to_edit; </code>
<?php
session_start();
include("header.php");
include("database.php");
include("sidebar.php");
include("delete.php");

include("edit.php");


echo "Id get : " ;
echo $_SESSION['id_to_edit'];
echo $id_to_edit;

Recognized by PHP Collective

New contributor

ola_099990 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật