如何将产品ID从一个页面传递到另一个页面,以便在PHP中插入该产品的图像?
There are two tables; one for product details and the second for product images.
The first page is product detail and product details page insert the product details. So how to fetch the id of that recent inserted product and that id redirect to another page for insertion of that product image?
I want to pass product id from one page to another page for insertion of that product image.
The id can pass through url but I don't know how to pass it.
Both tables are different for product detail and product image.
Please provide code for it.
In this page product detail inserted and id of this recent inserted product I want to pass in another page:
I want the product id here:
product-details.php
<?php
$result = mysqli_query($connection, $some_insert_query_here);
if($result){
$lastId = mysqli_insert_id($connection);
header("Location: product-image.php?id=".$lastId);
}
?>
product-image.php
<?php
if(isset($_GET['id'])){
$lastId = $_GET['id'];
// do stuff with this last inserted product id
}
?>
or try sessions:
product-details.php
<?php
$result = mysqli_query($connection, $some_insert_query_here);
if($result){
$lastId = mysqli_insert_id($connection);
session_start();
$_SESSION['last_id'] = $lastId;
header("Location: product-image.php");
}
?>
product-image.php
<?php
session_start();
if(isset($_SESSION['last_id'])){
$lastId = $_SESSION['last_id'];
// do stuff with this last inserted product id
}
?>
You can use $last_id = $conn->insert_id;
in plain PHP.
While in laravel you can use insertGetId()
function to get the id of last inserted Item.
And retrieve the last_id in other page by using $last_id=$_GET['id']
if method is get or $last_id=$_POST['id']
if method is post.
i think you can use php sessoins and pass the values to next pages and also don't forget to kill the session once u finished.
<?php
session_start();
$_SESSION["Product_id"] = your id;
$_SESSION["Product_iamge"] = your image url;
?>