How to make redirect to new detail data after submit form on PHP

user2967389

I have form for inserting data and store it into database. these table have field title,promoted, file ( field with name 'field' used for store url image which have uploaded).

Actually i want to implement case when i have success insert new data in that form, it will redirected to new data detail.

for example when i have inserted data with id = '10', then it will redirected to page which view detail data which id = '10' automatically after data save succesfully.

this is my following code

<form method="post" action="addpreview.php" name="formregister" enctype="multipart/form-data">

now my code on addpreview.php

include_once("classes/dbconfig.php");
include("classes/viral.php");

$viral = new Viral();

if(isset($_POST['Submit'])) {   
    $title = $viral->escape_string($_POST['title']);
    $promoted = $viral->escape_string($_POST['promoted']);



    // upload image

    if (($_FILES['photo']['name']!="")){
    // Where the file is going to be stored
    $target_dir = "upload/";
    $file = $_FILES['photo']['name'];
    $path = pathinfo($file);
    $filename = $path['filename'];
    $ext = $path['extension'];
    $temp_name = $_FILES['photo']['tmp_name'];
    $path_filename_ext = $target_dir.$filename.".".$ext;

    // Check if file already exists
    if (file_exists($path_filename_ext)) {
        echo "Sorry, file already exists.";
        }else{
            move_uploaded_file($temp_name,$path_filename_ext);
            echo "Congratulations! File Uploaded Successfully.";
        }
    }


    //insert data to database   
    $result = $viral->execute("INSERT INTO preview(title,promoted,file) VALUES('$title','$promoted','$path_filename_ext')");

    header('Location: index2.html');



}
glocke

If your preview table uses an auto-incrementing id field, you can get the new ID of the inserted record using MySQL's LAST_INSERT_ID function.

SELECT LAST_INSERT_ID()

From there, you can include this ID value in the redirect URL, e.g. index2.html?id=${id}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related