web.curious.gk
web.curious.gk

Reputation: 167

Check for GET value, POST from FORM in the same page does not work

I would like to ask some ideas and help on how i can fix this.

I have a php page where it loads a form but before the form loads the data from DB it check first if the data exist otherwise post error that record not found. here's the flow of the form. The form should be accessible by http://server/view.php?id=10

Code snippet:

// check id first from get
$id = intval($_GET['id']);

// do mysql query here then i check if exist
if ($id == 0) { die("No Record Exist in DB"); }

if ($_POST['SUBMIT'] != null) {
// do validation of data from form and insert record from DB //
// use redirect to same page here to http://server/view.php?id=10 to refresh with new data

//show the form here below.

Now my problem is i can do checking if data exist and it shows No Record Exist in DB but if record exist it shows the form with records. Now when I want to add new info and submit the form, instead of going to $_POST['SUBMIT'] it goes to checking and it receive No Record Exist in DB, what i observe is that when the page reload it only shows http://server/view.php in the url.

How can I fix it thanks.

Upvotes: 0

Views: 602

Answers (6)

MOOD
MOOD

Reputation: 111

what about this way:

$id = ($_GET['id']) ? $_GET['id'] : FALSE;
if($id != FALSE && $_POST['submited'] == 'yes') {
    // do anything
    // ...
    // ...
    // forward to view.php?id=$id
}

i always use a hidden field in forms to check if it has been submited or not (better soulution will be with tokens that change after each submit). so in this example you will need to add the following hidden field:

<input type="hidden" name="submited" value="yes" />

hope this helped and it was what you're searching for :)

Upvotes: 1

Aleks G
Aleks G

Reputation: 57306

I think there may be a logic problem in your code. When you're adding a new record, what parameter (if any) are you passing for the id when submitting the form with a new record? I would structure the code as follows:

// check id first from get
if(isset($_GET['id']))
{
    $id = intval($_GET['id']);
    // do db query to check if this ID exists
    $sql = "select ... from ... where id = $id";
    ...

    if(!exists) {
        die("No Record Exist in DB");
    }
    elseif(isset($_POST['SUBMIT'])) {
        // your ID exists, editing existing record
        // update your db record
        // then redirect to some confirmation page
    }
}
elseif(isset($_POST['SUBMIT'])) {
    // here ID is not set at all, this means, you're adding a new record
    // add your new record
    // then redirect to some confirmation page
}
else
{
    // show the form here - new or update based on the 'id' parameter
}

Upvotes: 1

Kris Van den Bergh
Kris Van den Bergh

Reputation: 1152

Checking if an input field is null or not is usually not a good idea.

The solution lies in checking if the request is of the type POST or not, i.e. if the form is submitted or not.

Try something like this:

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // here comes to execute once form is submitted
    } else {
    // check id first from get
    $id = intval($_GET['id']);

    // do mysql query here then i check if exist
    if ($id == 0) { die("No Record Exist in DB"); }

    // show form here
    } 
    ?>

Good luck!

Upvotes: 0

cppit
cppit

Reputation: 4564

You have to include the link in your form action for example : method="post" action="view.php?id=idnumberhere"> that should post the id number to the page and $_POST['id'] will retrieve it

of course since your id is in php you want to use it as a variable

Upvotes: -2

Fabricio Bedeschi
Fabricio Bedeschi

Reputation: 101

try isset($_POST['SUBMIT']) && $_POST['SUBMIT'] != null and isset($_GET['id'])

Upvotes: 1

samn
samn

Reputation: 2807

You should check first if your id is set, then check if there's a corresponding record in the database.

Upvotes: 0

Related Questions