0

Is it possible to put a row ID in header()? I tried to put header() inside an if statement but I always got a message of:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

Here's my code:

$result = mysqli_query($db_con,$query);
$target="Admin/upload/otherPic/".basename($_FILES['image']['name']);
if ($result){
    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
        echo "<script>
            alert('Record updated successfully!');
            </script>";
        header("Location: admin-viewacc.php?id='$result['id'];'");
    }
    else{
        echo "Error updating record: " . mysqli_error($db_con);
    }
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Jhace Young
  • 13
  • 1
  • 5

2 Answers2

1

Try using,

header("Location: admin-viewacc.php?id=" . $result['id']);

And it is good practise to use exit statement after header($string), because it can get a little messy sometimes.

header("Location: admin-viewacc.php?id=" . $result['id']);
exit;

Read this - header is not working when called within a function

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
1

You need to fetch the recordset ( presuming it is a select query ) and there is no need for additional single quotes around the ID in the header string.

This is liable to errors - if you output any html, whitespace etc before calling certain PHP functions you will trigger an error. To that end, do not add the javascript code - instead display a message on the next page.

if( $result ){

    if( move_uploaded_file( $_FILES['image']['tmp_name'], $target ) ){

        $result = mysqli_query( $db_con, $query );
        while( $rs=$result->fetch_object() )$id=$rs->id;


        $target = 'Admin/upload/otherPic/' . basename( $_FILES['image']['name'] );


        echo "
            <script>
                alert('Record updated successfully!');
            </script>";

        header( "Location: admin-viewacc.php?id={$id}" );

    } else{
        echo "Error updating record: " . mysqli_error( $db_con );
    }
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46