Forum Moderators: coopster

Message Too Old, No Replies

Creating SQL from database fields

Creating SQL from database fields

         

skinsey

10:16 am on Oct 19, 2009 (gmt 0)

10+ Year Member



Can somone look over this code and let me know what I'm doing wrong, or how to accomplish what I'm trying to do.
Thanks.

The inventory table contains 1000's of records and the fieldnames are randomly generated. It the takes the field fieldname and alters the table of dailyinventory. Now I trying to figure out how to insert into the dailyinventory table since the fields and post names can't be determined.

Currently insert does work for all fields except the fields gernerated from this line of code.
$sqlvariable = "$sqlvariable'$".$row[fieldname]."',";

Here is the output to the screen when I echo $sql

For the field like 8VUGWL that is the name of field in db and of post. The value entered is a number.

INSERT INTO dailyinventory(T5N89B,QA3G1C,8VUGWL, company, enterdate,center,estimatedtotal) VALUES ('$T5N89B','$QA3G1C','$8VUGWL', 'The Company','2009-10-19','Tampa Bay','100')

Here's the total code.

// Get post
extract($_POST);

// Get fieldname to generate query
$query = "SELECT fieldname,company, effectivedate , enddate , sortorder FROM inventory WHERE company = '$company' AND effectivedate <= '$date' AND (enddate > '$date' OR enddate IS NULL) ORDER BY sortorder";

$temp = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($temp))
{
$sqlfields = "$sqlfields".$row[fieldname].",";
$sqlvariable = "$sqlvariable'$".$row[fieldname]."',";
}

//finished SQL code for insert
$sql = "INSERT INTO dailyinventory($sqlfields company, enterdate,center,estimatedtotal) VALUES ($sqlvariable'$company','$date','$center','$total')";

$temp1 = mysql_query($sql)
or die(mysql_error());

skinsey

9:24 pm on Oct 19, 2009 (gmt 0)

10+ Year Member



Never mind figured out how to do it.