0

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');



}
  • What is exactly the problem? (the redirect won't work, because you have output [the `echo`] before) – Jeff May 30 '17 at 14:14
  • You have to obtain the inserted id from `$viral` (I do not know, what class it is and if it can get the last inserted id) and then use something like `header('Location: detail.php?id='.$id);` – Roman Hocke May 30 '17 at 14:15
  • what is $viral , any how if you can get `$id = $viral->getLastInsertId();header('Location: detail.php?id='.$id);` – Megan Fox May 30 '17 at 14:20
  • Headers must always be send before any output! `echo()` is output! You could instead turn output buffering on: `ob_start(); echo "..."; header('Location: ... '); ob_end_flush();`. However, since you're redirecting to another page, I don't rly see the point. I would just remove the echo instead. – icecub May 30 '17 at 14:20
  • On a side note: It looks to me like your script is wide open to SQL injection and a very insecure upload script. How would it stop me from uploading a PHP script with an `exec()` command and do some serious damage to your server? Take a look at my answer to this question here. It will help you secure your script: https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921 – icecub May 30 '17 at 14:30

1 Answers1

0

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}

glocke
  • 16
  • 1