如何使用变量而不是字段名称更新sql字段?

I'm new here (and not english guy, obviously), but I have a problem.
I have a SQL request, it's an UPDATE like the following :

 $rep = $bdd->exec("UPDATE z_agenda SET AGENDA_1='$code'WHERE AGENDA_NOM='$agent' AND AGENDA_TYPE='code'");

BUT, and now the fun is incoming, I want to change AGENDA_1 to a variable which can contains AGENDA_1, AGENDA_2, etc. until AGENDA_31.
But it seems SQL doesn't like it.
So, anybody has an idea? I'm completely stuck right now.

If you want more explanations, I'm here. Sit, wait, and read some help forum

I'm adding some few code : "

 $mois = $_POST['mois'];   (integer)
    $debut = $_POST['debut'];  (integer : 1-31)
    $lettre = $_POST['lettre'];   (integer)
    $couleur = $_POST['couleur'];   (integer)
    $agent = $_POST['agent'];   (string)
    $code = $lettre + $couleur;

    $rep = $bdd->exec("UPDATE z_agenda
        SET AGENDA_1='$code'
            WHERE AGENDA_NOM='$agent'
            AND AGENDA_TYPE='code'");   

"

my database contain few information columns, and 31 columns for each day. One line/month/user don't know how manage my database with an other solution.

There's actually quite a lot going on here.

  • you should consider using prepared statements to prevent SQL injection vulnerabilities;
  • you should read up on database normalization;
  • you could expand the string and add the columns dynamically using, for example, a for loop but you don't want to do this!

Having numbered columns is usually a Very Bad Idea. Click the database normalization link for detailed information and thorough guidelines on how to proceed. Your application will get unmaintainable with a database structure like this. You'll be writing 'string building loops' for the rest of your life, whereas problems like the one you're having now have been solved a million times before.

The loop is static just for an example but you can set the inner code into your accordingly

for($i=0;$i<=31;$i++)
{
    $agenda_coloumn = 'AGENDA_'.$i;
    $rep = $bdd->exec("UPDATE z_agenda SET $agenda_coloumn = '$code' WHERE AGENDA_NOM='$agent' AND AGENDA_TYPE='code'");
}