sql >> Base de Datos >  >> RDS >> Mysql

Variable dinámica de actualización de consulta de formulario POST de php

solo necesita crear una matriz de actualización dinámica. Algo como esto:

$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