Jump to content

using php variables as a MySQL column name


Snewzzer

Recommended Posts

Hi, This is my first posting on a forum so hope I don't break too many (none hopefully!) etiquette rules. I am new to both PHP and MySQL and would appreciate any help that could be given with my problem.

 

I have a form that posts values (mark1, mark2, etc) to the script that I am having the problem with. This script then uses a FOR loop to assign these into an array - $servicearr[ ] in this case. This all works fine and I can echo these values to test them.

 

I have also used a FOR loop to assign my MySQL column names into an array - $colarr[ ] in this case. Again this works and the values can be echoed to test it.

 

The problem occurs when using an UPDATE query, it doesn't understand the column name variables. The script is below :-

 

<?php

SESSION_START();

require("servicedb_con.php");

 

if(!$_SESSION['UserIsLoggedIn'])

    {

    header("location:1_techloginform.php");

    die();

    }

 

$fleetnum = $_SESSION['FleetNo'];

 

for($var=1;$var<300;$var++)

    {    $servicearr[$var] = $_POST['mark'.$var];    }

 

$sql = "SELECT * from truck_serv";

$result = mysql_query($sql);

 

for($var=1;$var<300;$var++)

    {    $colarr[$var] = mysql_field_name($result,$var);    }

 

for($var=1;$var<300;$var++)

{

if(!$servicearr[$var] == NULL)

    {

echo $colarr[$var]."<br/>";

    $sql = mysql_query('UPDATE truck_serv SET seatbelt = "'.$servicearr[$var].'" WHERE Fleet_No = "'.$fleetnum.'"');

    if(!$sql)

        {

        die('Could not query: '. mysql_error());

        }

    }

}

 

header("location:8B_truck_serv.php");

 

?>

 

The error I get is :-

Could not query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '".$colarr[$var]." = "S" WHERE Fleet_No = "DK07 CKA"' at line 1

 

Fleet_No is a column name and DK07 CKA its value (these are vehicles).

"S" is the correct value (my code for serviceable) supplied from the post form.

The only problem I can see is that an extra apostrophe has crept in at the start of my column name variable i.e. '".$colarr[$var]."

I have retyped the script completely and get the same error.

If I manually insert the column name it works fine. The trouble with that is I would have to write the same code out 300 times with a different column name in each time as the script update a vehicle servicesheet database .

 

Hope someone can help.

Thanks

 

 

Link to comment
Share on other sites

SORRY, many apologies, error in code supplied...

 

$sql = mysql_query('UPDATE truck_serv SET seatbelt = "'.$servicearr[$var].'" WHERE Fleet_No = "'.$fleetnum.'"');

 

This should read :-

 

$sql = mysql_query('UPDATE truck_serv SET " '.$colarr[$var].' " = " '.$servicearr[$var].' " WHERE Fleet_No = " '.$fleetnum.' " ');

 

and yes, this part of the code is the problem as when the column name is 'seatbelt' it works, when it uses the variable it doesn't.

Link to comment
Share on other sites

This doesn't seem wrong either. Can you change it to:

 

$query = 'UPDATE truck_serv SET " '.$colarr[$var].' " = " '.$servicearr[$var].' " WHERE Fleet_No = " '.$fleetnum.' " ';
$sql = mysql_query($query) or trigger_error(mysql_error(). ": $query",E_USER_WARNING);

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.