0

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)."'
baubali
  • 3
  • 3
  • I read your post triple times, but still, I don't get what you want to do. Try re-phrasing your question. – Denis V Nov 20 '16 at 19:09
  • i have a query that need to be modified dynamicly, the languages fiels and their values are changing if i add in the languages table more languages – baubali Nov 20 '16 at 19:28
  • So you already have a variable for each language, which has the same name as the language? How I read your query now, you are just doing english = english while you want to do english = $english, right? – JonathanStevens Nov 20 '16 at 19:34
  • yes, it is correct – baubali Nov 20 '16 at 21:54
  • You are **wide open** to SQL injection attacks. Use prepared/parameterized queries with PDO or similar to avoid this problem. – Brad Nov 20 '16 at 23:58

0 Answers0