Claus Hjort Bube
Claus Hjort Bube

Reputation: 55

Dynamically create $_POST lines i php

How do I dynamically create $_POST lines in php, based om extractions from MYSQL.

First off, my table is flat, meaning I have every fieldnames in one column (field) and all the fieldtext in another column (text).

I'm trying to use this code:

require($_SERVER["DOCUMENT_ROOT"]."/NR/func/mysql-funktioner.php");
$site = trim ((!empty($_POST['site'])) ? $_POST['site'] : $_GET['site'] );

function update($text,$field,$site) {
    $sql = "UPDATE nr_site SET text = $text WHERE field = $field AND site = $site LIMIT 1";
    aabn_forbindelse_og_vaelgdb();
    sql_spoerg($sql);
    luk_forbindelse();
}

$sql_site = "SELECT field
             FROM nr_site 
             WHERE site ='".$site."'"; 

aabn_forbindelse_og_vaelgdb();
$resultat_site = sql_spoerg_og_faa_svar($sql_site);
luk_forbindelse();
for($i=0;$i<count($resultat_site);$i++) {
    extract($resultat_site[$i]);
    $HTTP_VARS = 'trim((!empty($_POST[\''.$field.'\']))?$_POST[\''.$field.'\']:$_GET[\''.$field.'\'])';
    $text = eval($HTTP_VARS);   

    //update($text,$field,$site);
    echo $text;
}

But it gives me

Parse error: syntax error, unexpected $end in /customers/kalna.dk/kalna.dk/httpd.www/NR/func/save.php(29) : eval()'d code on line 1 

instead of the value passed from the form. There's no problem if I manually write the fieldnames like:

$HTTP_VARS = trim ((!empty($_POST['feature_heading'])) ? $_POST['feature_heading'] : $_GET['feature_heading'] );

$text = $HTTP_VARS;

Do you have some advises?

Upvotes: 1

Views: 304

Answers (2)

Phoenix
Phoenix

Reputation: 4536

Where does $field come from down below in the eval()? Should it be $field = extract($resultat_site[$i]);? Oh, I see, extract automatically creates variables from the array keys, never used that function before. Is eval necessary for your code? It might be best if you just did:

$HTTP_VARS = trim ((!empty($_POST[$field]))?$_POST[$field]:$_GET[$field]);
$text = $HTTP_VARS;

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799370

Variables can be used as array indexes.

echo $_POST[$field];

Upvotes: 1

Related Questions