Hello guys (sorry for my bad english in advance). Im completely new in php so my code is a huge mess, but 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 im 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 cant see this variable at all!
There are solutions I have alredy tried that didnt work for me:
- Creating session variable and sesssion_start() -> edit-products-admin.php cant see it at all
- using peace of edit.php in my edit-products-admin.php code -> $id_to_edit didnt have any value at all, but session variable worked. Unfortunately couldnt change it or unset later
- Using header with variable in it -> i could see right id on top of the page but code couldnt see it at all.
Here are the pieces of code, Im not uploading the whole scripts because there was a lot of unnesessary info, but if you need it – please ask and Ill be happy to send it. Sorry in advance for any stupid desicions … 🙂
edit.php –
//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
<?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
<?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;
Oleksandra Simeonidi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.