Jump to content

Mysql values clause, variable values from file


mjsnosk8

Recommended Posts

Hi all,

 

Just curious why this works:

 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
$import="INSERT into $prodtblname ($csvheaders1) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";
}
And this does not:

$headdata_1 = "'$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]'";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
$import="INSERT into $prodtblname ($csvheaders1) values($headdata_1)";
}

it puts $data[#'s] in the database fields instead of the actual data that '$data[0]','$data[1]'... relates to.

 

I wrote a script to create the values in $headdata_1 based on the number of headers in $csvheaders1 but can't seem to get it working in the sql statement.

 

Thanks

Link to comment
Share on other sites

Hey thanks for the fast reply. Got me looking in the right direction. My work around is:

 

User saves csv formated headers in the "profiles" table from a form.

 

then I:

            $fo_w = fopen($H1datafile, 'w');

fwrite($fo_w, '');

fclose($fo_w);

$fo_a = fopen($H1datafile, 'a');

$count = substr_count($header_row_1, ',') ;

$i=0;

while($i<=$count)

{

$data = "\'".'$data[' . $i . ']'."\',";

$i++;

fwrite($fo_a, $data);

}

fclose($fo_a);

$read = file($H1datafile);

foreach($read as $line){

$header_data_1 = substr($line, 0, -1);

                          }

And store the value clause string $header_data_1 into the db.

 

Later in the page I:

 

                            $H1dataphp = 'temps/H1data.php';

$fo_w = fopen($H1dataphp, 'w');

 

$line1= '<?php while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){'."\n";

fwrite($fo_w, $line1);

$line2 = '$insert = "INSERT into r_source_feed_1 ('.$header_row_1.') values('.$header_data_1.')";'."\n";

fwrite($fo_w, $line2);

$line3 = 'mysql_query($insert) or die(mysql_error()); } ?>';

                            fwrite($fo_w, $line3);

fclose($fo_w);

Then include H1data.php on the page I need it.

Now you can enter any number of headers in the text field and insert just those columns from a linked text file.

MJ :D :D

Link to comment
Share on other sites

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.