dongzhi1851 2016-11-21 00:00
浏览 52
已采纳

php POST表单查询更新动态变量

i have this form

<form method="post" action="process.php">
    Name: <input type="text" name="name" value="">
    <br />
    English: <input type="text" name="english" value="">
    <br />
    French: <input type="text" name="french" value="">
    <br />
    <input type="submit" name="submit" value="Submit">  
</form>

and we make this query on process.php

$query = "
        UPDATE
        `translations_structure`
        SET
        `updated_on` = '".time()."',
        `english` = '".utf8_encode($english)."',
        `french` = '".utf8_encode($french)."'
        WHERE
        `id` = '".$id."'";

and if i edit the table languages and add more languages the form dynamically will modify to lets say this example

<form method="post" action="process.php">
    Name: <input type="text" name="name" value="">
    <br />
    English: <input type="text" name="english" value="">
    <br />
    French: <input type="text" name="french" value="">
    <br />
    Spanish: <input type="text" name="spanish" value="">
    <br />
    German: <input type="text" name="german" value="">
    <br />
    <input type="submit" name="submit" value="Submit">  
</form>

and the query i need to dynamically be edited

$query = "
        UPDATE
        `translations_structure`
        SET
        `updated_on` = '".time()."',
        `english` = '".utf8_encode($english)."',
        `french` = '".utf8_encode($french)."',
        `spanish` = '".utf8_encode($spanish)."',
        `german` = '".utf8_encode($german)."'
        WHERE
        `id` = '".$id."'";

what i don't understand is how i make this dynamically inside the query the code

*the name of the form field is the same of the name of the variable i POST
*and the name of the column from the table is the same of the name of the POST

    `english` = '".utf8_encode($english)."',
    `french` = '".utf8_encode($french)."',
    `spanish` = '".utf8_encode($spanish)."',
    `german` = '".utf8_encode($german)."',
    `other_language` = '".utf8_encode($other_language)."',
    `other_language2` = '".utf8_encode($other_language2)."'

here above i have explained how i make the query but i cant understand how to write the variables

I know is a little bit difficult what i need but maybe someone understand what i need

thank you

Above this line is the edited message because someone flagged this message answered





I will explain first what i want to do:

I have a table called "translations" where i store the languages. ex: english, french, spanish, etc.
I use a form to update the new values, the problem is that i want to doit dynamic not to insert this query on every php file manually because the languages table will grow or edit and i want to work dynamically not to edit every php file.
the variables name are the same like fields name in data base i manage to make an array for the names on table translations

this is what i have until now to make it dynamic the problem is i don't know how to insert variables in the query $_POST['english'], $_POST['french'], etc

$db = new DB();
$query = $db->query("SELECT * FROM `translations_languages` ORDER BY `name` ASC");
while($row = $query->fetch_assoc()){
    $values[] = "`{$row['name']}` = '{$row['name']}'";
}

    $dynamic_result = "".strtolower(implode(",", $values))."";
    $query = "
    UPDATE
    `translations_structure`
    SET
    `updated_on` = '".time()."',
    $dynamic_result
    WHERE
    `id` = '".$id."'
    ";

echo "$query";

and this is how the query looks normaly

$query = "
            UPDATE
            `translations_structure`
            SET
            `updated_on` = '".time()."',
            `english` = '".utf8_encode($english)."',
            `french` = '".utf8_encode($french)."',
            `spanish` = '".utf8_encode($spanish)."'
            WHERE
            `id` = '".$id."'";

i want to add to the query this values

`english` = '".utf8_encode($english)."',
`french` = '".utf8_encode($french)."',
`spanish` = '".utf8_encode($spanish)."'
  • 写回答

1条回答 默认 最新

  • dqrl3595 2016-11-21 09:31
    关注

    you just need to create a dynamic update array. Something like this:

    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容