php POST表单查询更新dinamic变量 [英] php POST form query update dinamic variable

查看:154
本文介绍了php POST表单查询更新dinamic变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表单

 < form method =postaction =process.php> 
名称:< input type =textname =namevalue =>
< br />
英文:< input type =textname =englishvalue =>
< br />
法语:< input type =textname =frenchvalue =>
< br />
< input type =submitname =submitvalue =Submit>
< / form>

,我们对process.php进行此查询

  $ query =
更新
`translations_structure`
SET
`updated_on` ='.time() ,
`english` ='.utf8_encode($ english)。'',
`french` ='.utf8_encode($ french)。''
WHERE
` id` ='。$ id。';

如果我编辑表语言并添加更多的语言,表单将动态修改,让我们说这个例子

 < form method =postaction =process.php> 
名称:< input type =textname =namevalue =>
< br />
英文:< input type =textname =englishvalue =>
< br />
法语:< input type =textname =frenchvalue =>
< br />
西班牙语:< input type =textname =spanishvalue =>
< br />
德语:< input type =textname =germanvalue =>
< br />
< input type =submitname =submitvalue =Submit>
< / form>

,我需要动态修改查询

  $ query =
更新
`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)。'',$ b $ ``.utf8_encode($ french)。'',
`spanish` ='.utf8_encode($ spanish)。'',
`german` ='.utf8_encode $,
`other_language` ='.utf8_encode($ other_language)。'',
`other_language2` ='.utf8_encode($ other_language2)'

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



我知道有点困难我需要什么,但也许有人明白我需要什么



谢谢



Abov这条线是编辑的消息,因为有人举报了这条消息回答了











我将首先解释我想要做什么:


我有一个名为翻译的表格我在哪里存储语言。例如:英语,法语,西班牙语等。

我使用一个表单来更新新的值,问题是我想做动态不要手动插入每个php文件上的这个查询,因为语言表将增长或编辑,我想要动态地编辑每个php文件。

变量名称与数据库中的字段名称相同
i管理为数组创建名称在桌面上翻译



这是我现在有的让它动态的
问题是我不知道如何在查询中插入变量$ _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 =
更新
`translations_structure`
SET
`updated_on` ='.time()。'',
$ dynamic_result
WHERE
`id` ='$ id。'
;

echo$ query;

这是查询查询正常的方式

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

我想添加到查询中这个值



$,
`french` ='.utf8_encode($ french)。'',
`spanish` ='.utf8_encode($ spanish)'


解决方案

你只需要创建一个动态更新数组。这样的东西:

  $ languagesToUpdate = array(); 

//这是一个例子,您应该修改为脚本:

//创建一个变量/常量,以确保只更新允许的字段
$ allowedLanguages = array('english'=> true,'french'=> true,'spanish'=> true,'german'=> true,'other_language'=> true)

//迭代扔帖子并检查允许的语言并添加到languagesToUpdate我们需要更新的语言,值为
foreach($ _POST as $ post => $ value){
if(isset($ allowedLanguages [$ post])&& $ allowedLanguages [$ post]){
$ languagesToUpdate [] ='`'。 $帖子。 '`=''。utf8_encode($ value)。'';
}
}

//添加其他数据,如updated_on
$ languagesToUpdate [] ='`updated_on` ='。时间() 。 ;

//更新数据库
$ db ='UPDATE`translations_structure` SET'.implode(',',$ languagesToUpdate)'WHERE`id` ='。(int)$ id ;

//这样会产生这样的东西:
// UPDATE`translations_structure` SET`english` =英文文本,西班牙文=西班牙文,`updated_on` = 1479720637 WHERE`id` = 1


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)."'

解决方案

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

这篇关于php POST表单查询更新dinamic变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆