sql >> Database >  >> RDS >> Mysql

php POST formulier query update dynamische variabele

u hoeft alleen een dynamische update-array te maken. Zoiets als dit:

$languagesToUpdate = array();

// this is an example, you should modify as your script:

// create a variable/constant to make sure you update only allowed fields
$allowedLanguages = array('english' => true, 'french' => true, 'spanish' => true, 'german' => true, 'other_language' => true);

// iterate threw post and check for allowed languages and add to languagesToUpdate the language we need to update with it's value
foreach ($_POST as $post => $value) {
    if (isset($allowedLanguages[$post]) && $allowedLanguages[$post]) {
        $languagesToUpdate[] = '`' . $post . '` = "' . utf8_encode($value) . '"';
    }
}

// add additional data like updated_on
$languagesToUpdate[] = '`updated_on` = ' . time() . '';

//update database
$db = 'UPDATE `translations_structure` SET '.implode(', ', $languagesToUpdate).' WHERE `id` = '.(int)$id;

// this will produce something like this:
// UPDATE `translations_structure` SET `english` = "English text", `spanish` = "Spanish text", `updated_on` = 1479720637 WHERE `id` = 1



  1. DB-ontwerp:ledentafel apart of alles in één tafel?

  2. Verwijder een record van de console -- Ruby on Rails

  3. Hoe zelf-gehoste wordpress en geïnstalleerde plug-ins van live site upgraden naar de nieuwste beschikbare versies zonder problemen te ondervinden?

  4. Grote CSV importeren in mysql-database