GET elements in the url are missing after after submitting in POST format

3798 views php mysql if-statement
10

here is my code

if(isset($_GET['id']) && $_GET['id']!=''){

$id= get_safe_value($con, $_GET['id']);
$res=mysqli_query($con, "select * from `categories` where id='$id'");
$row=mysqli_fetch_assoc($res);
$categories=$row['categories'];
}


if(isset($_POST['submit'])){

$categories= get_safe_value($con, $_POST['categories']);
if(isset($_GET['id']) && $_GET['id']!=''){
    mysqli_query($con, "UPDATE `categories` SET `categories`='$categories' WHERE `categories`.`id`=$id");
}else{
    mysqli_query($con,"INSERT INTO `categories` ( `categories`,`status` ) VALUES ('$categories', '1')");
}
echo "<script>window.location.href='categories.php'</script>";
die();
}

to edit my categories, i sent id to the url like this

echo "<a href='manage-categories.php?id=".$row['id']."'>Edit </a>"; 

but after form submission, that second if block unable to catch the id. Any solution?

answered question

You said it yourself... If the HTML form has a POST type you have to make your handling in $_POST and if it was in GET then you should use $_GET... Please, provide the HTML form or more code so that we can understand how you are submitting the POST/GET request

1 Answer

6

GET and POST values don't persist between requests. When you submit a form, you're making a new request to a new URL.

To ensure the value be sent again when submitting, you can either:

  1. put the querystring (GET) parameter into the URL you send to when submitting the form e.g. by injecting it into the form's "action" attribute.

Or,

  1. create a hidden field in your form which contains the relevant ID, and then make your PHP code search for $_POST["id"] instead of the GET version.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories