php POST表单查询更新dinamic变量

时间:2016-11-21 00:00:38

标签: php mysql forms variables dynamic

我有这个表格

<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>

我们在process.php

上进行此查询
$query = "
        UPDATE
        `translations_structure`
        SET
        `updated_on` = '".time()."',
        `english` = '".utf8_encode($english)."',
        `french` = '".utf8_encode($french)."'
        WHERE
        `id` = '".$id."'";

如果我编辑表格语言并添加更多语言,表单将动态修改为例如

<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>

我需要动态编辑查询

$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."'";

我不明白的是我是如何在查询代码中动态制作的 *表单字段的名称与变量i POST的名称相同 *并且表中列的名称与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)."'

上面我已经解释了我如何进行查询,但我无法理解如何编写变量

我知道我需要的东西有点困难,但也许有人明白我的需要

谢谢你

此行以上是已编辑的消息,因为有人标记了此消息的回答




我先解释一下我想做什么:

我有一个名为“翻译”的表,我存储语言。例如:英语,法语,西班牙语等。
我使用表单来更新新值,问题是我想动态不要手动在每个php文件上插入此查询,因为语言表将增长或编辑,我想动态工作而不是编辑每个php文件。
变量名称与数据库中的字段名称相同 我设法为表格翻译上的名字制作一个数组

这是我迄今为止的动态 问题是我不知道如何在查询$ _POST ['english'],$ _POST ['french']等中插入变量

$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";

这就是查询看起来正常的方式

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

我想在查询中添加此值

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

1 个答案:

答案 0 :(得分:0)

您只需要创建一个动态更新数组。像这样:

$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