Samuel Meddows
Samuel Meddows

Reputation: 36742

Building a dynamic $_POST statement

ingHey guys.

I am wonder the correct syntax for using a $_POST statement in a while loop.

I have written this.

$result_i = $_POST['result_i'];
while ($result_i > 0){
        //Get Post Values
        $driver = $_POST['driver_update_".$result_i."'];
        $BookingID = $_POST['ID_".$result_i."'];
        $Task_No_update = $_POST['Task_No_update_".$result_i."'];

        //SQL 
        $driver_update = mysql_query("UPDATE booking SET driver = '$driver', TaskNo= '$Task_No_update' WHERE BookingID = '$BookingID' " );
}

The problem I have is:

$_POST['driver_update_".$result_i."'];

Is it possible to write $_POSTS statements in this way.

Cheers.

Upvotes: 1

Views: 781

Answers (2)

Ortiga
Ortiga

Reputation: 8824

You don't need to wrap everything in quotes here

$driver = $_POST["driver_update_" . $result_i];
$BookingID = $_POST["ID_" . $result_i];
$Task_No_update = $_POST["Task_No_update_" . $result_i];

Upvotes: 1

Phil
Phil

Reputation: 164979

The problem is you cannot interpolate variables in single-quoted strings.

Try concatenation instead

$_POST['driver_update_' . $result_i]

or use double-quotes and variable enclosures

$_POST["driver_update_{$result_i}"]

See http://www.php.net/manual/en/language.types.string.php


Also, that looks like an infinite loop as $result_i never changes.

Upvotes: 2

Related Questions