Jump to content

[SOLVED] Problem Passing Variables Part(2)


PHPNewbie55

Recommended Posts

Please take a look at this

<?php
$col_keys = mysql_query("SELECT * FROM $mysqltbl");
while($mysql_keys = mysql_fetch_field($col_keys)) {
if ($_POST["$mysql_keys->name"] > ""){
$keys .= "'\$data[".($_POST["$mysql_keys->name"])."]', ";
} else {
$keys .= "";
}
?>

 

OK Now...

How would I pass the:

$keys .= "'\$data[".($_POST["$mysql_keys->name"])."]', ";

 

onto the next section...

See how I am trying to create an instance of '$data[0]' for each $keys?

 

<?php
while ($data = fgetcsv ($feed, 10000, ",")) {
mysql_query("INSERT INTO $mysqltbl (
$cols some_bs
) VALUES (
$keys 'some_bs'
)");
}
?>

 

And make it insert the new data..??

 

 

The above code works but what I get inserted into the MySQL DataBase is $data[0] $data[1] $data[2] etc...

They are in the right spots... its just that the script is not processing the info as part of the script but text...

 

Hope that makes sense...

Any help would be appreciated!

 

Merry Christmas!!!!!

 

 

Link to comment
Share on other sites

Oh .. I get it.  You are generating php code.  You can do something like this:

 

$code = "$data[0] = 'foo';";
eval($code);
print $data[0] . "\n";

 

But really, it would be better if you rewrote the program logic so you didn't need to do something so awkward.

Link to comment
Share on other sites

ok, in PHP (well and most other web middleware products) you cant variables are not persistent across pages.

 

there are 2 ways to pass values between pages,

1. post/get arguments

2. session vars

 

for larger data sets use sessions

 

at the top of each php file add session_start()

 

<?php
ob_start()
session_start();

 

then in your code you set session vars. example

 

sending.php

$_SESSION['keys'] = $keys;

 

receiving.php

if (!isset($_SESSION['keys'])) {
ob_clean();
   	header('HTTP/1.0 302 Found');
   	header('location: /sending.php');
} else {
    $keys = $_SESSION['keys'];
}

 

hope this helps

 

Link to comment
Share on other sites

OK here is the first page.. well actually the second... the first is just a form that allows me to input the PassWord - MYSQL Database - MYSQL TABLENAME etc...

 

<?php

} elseif ($action == "submit") {
//   require("settings.php");
$mysqlserver == ($_POST["mysqlserver"]);
$mysqllogin ==  ($_POST["mysqllogin"]);
$mysqlpassword == ($_POST["mysqlpassword"]);
$mysqldb == ($_POST["mysqldb"]);
$mysqltbl == ($_POST["mysqltbl"]);
$csvfile == ($_POST["CSV_File"]);
$CSV_File = htmlspecialchars($csvfile);
// MAKE CONNECTION
$conn  = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword");

// IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE
if (!$conn)
  {
  die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error());
  }
  
mysql_select_db("$mysqldb", $conn);

######  GET CSV COLUMN NAMES
#################################################################################
$feed = fopen($CSV_File, 'r');  
$csv = file($CSV_File);

$columns = explode(",", $csv[0]);
$csv_column_names = implode("<br>", $columns);
$options = "";
foreach ($columns as $value){
list($key) = each($columns);
$options .= "<option value=\"$key\">$value</option>"; }


print "
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" >
  <tr>
    <td><b>Match Up The MySQL Columns With The CSV Columns</b></td>
  </tr><form method=\"post\" action=\"import_csv.php?action=import\">
  <input type=\"hidden\" name=\"mysqlserver\" value=\"$mysqlserver\"/>
  <input type=\"hidden\" name=\"mysqllogin\" value=\"$mysqllogin\"/>
  <input type=\"hidden\" name=\"mysqlpassword\" value=\"$mysqlpassword\"/>
  <input type=\"hidden\" name=\"mysqldb\" value=\"$mysqldb\"/>
  <input type=\"hidden\" name=\"mysqltbl\" value=\"$mysqltbl\"/>
  <input type=\"hidden\" name=\"CSV_File\" value=\"$CSV_File\"/>
  ";

######  GET MYSQL COLUMN NAMES
#################################################################################
$mysql_column_names = "";
$get_columns = mysql_query("SELECT * FROM $mysqltbl");
while($mysql_columns = mysql_fetch_field($get_columns)) {

print "<tr>
    <td>
<div style=\"padding:5px; border-bottom:1px solid #cccccc;\">
 CSV Column: <select name=\"".$mysql_columns->name."\"><option value=\"\"></option>$options</select> Should Be Inserted Into <b>".$mysql_columns->name."</b><br>
</div>
</td>
  </tr>";


}
print "<tr><td><center><input type=\"submit\" value=\"submit\" /></center></td></tr></table></form>";


?>

 

The Next Page.....  The one I am having problems with....

<?php
} elseif ($action == "import") {
$mysqlserver == ($_POST["mysqlserver"]);
$mysqllogin ==  ($_POST["mysqllogin"]);
$mysqlpassword == ($_POST["mysqlpassword"]);
$mysqldb == ($_POST["mysqldb"]);
$mysqltbl == ($_POST["mysqltbl"]);
$csvfile == ($_POST["CSV_File"]);
$CSV_File == htmlspecialchars($csvfile);
$feed = fopen($CSV_File, 'r');
// MAKE CONNECTION
$conn  = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword");
// IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE
if (!$conn){die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error());}
mysql_select_db("$mysqldb", $conn); 

$col_name = mysql_query("SELECT * FROM $mysqltbl");
while($mysql_col = mysql_fetch_field($col_name)) {
if ($_POST["$mysql_col->name"] > ""){

////  if the column field is empty don't include it........
$cols .= "".$mysql_col->name.", ";
} else {
$cols .= "";
}

}
$col_keys = mysql_query("SELECT * FROM $mysqltbl");
while($mysql_keys = mysql_fetch_field($col_keys)) {

/// If the key is not empty include it........ else don't include it........
if ($_POST["$mysql_keys->name"] > ""){




///  PROBLEM AREA..........
$keys .= "'\$data[".($_POST["$mysql_keys->name"])."]', ";
} else {
$keys .= "";
/// END PROBLEM AREA.............



}
}
print "INSERT($cols some_bs) INTO ($keys 'some_bs')<hr>";

while ($data = fgetcsv ($feed, 10000, ",")) {
mysql_query("INSERT INTO $mysqltbl (
$cols some_bs
) VALUES (
$keys 'some_bs'
)");
}

?>

 

The only way that I can get the POSTED info is to match up the MySQL Coumn Names with the individual

$_POST["$mysql_keys->name"]

Which works and it's the only way that I can figure out to get the POSTED info on the second page...

Because I don't know what the $NAME == ($_POST["NAME"]); Info is....

 

And they must match up to the MySQL Column Names in order to be imported into the correct column....

 

I can't figure out how to use the eval() to run this portion.... (Never used eval() before..)

$keys .= "'\$data[".($_POST["$mysql_keys->name"])."]', ";

 

When The script gets to this portion it works...

<?php

while ($data = fgetcsv ($feed, 10000, ",")) {
mysql_query("INSERT INTO $mysqltbl (
$cols some_bs
) VALUES (
$keys 'some_bs'
)");
}
?>

 

But what ends up in the MySQL Columns is the::::::

ID    COLUMN1    COLUMN2      COLUMN3      COLUMN4      COLUMN5

--------------------------------------------------------------------

1 ||  $data[0]  ||  $data[1]  ||  $data[2]  ||  $data[8]  ||  $data[7]

 

All of the instances of $data[?] are in the correct MySQL Columns... and there are the correct number of rows created from the CSV data....

so if I could just get it to eval($keys) during the process..  it should insert the CSV Data..

 

I hope that makes sense.....

 

Thanks for all your help.......!!!!!!!!

 

Link to comment
Share on other sites

I have tried both suggestions several times and niether works correctly... ( or I am doing something wrong.. like I said I have never used eval() before.. )

 

Just wondering if I should just start the whole thing over from scratch and try to work better logic into it so that I can get the CSV data mapped to the MySQL database...??

 

It would suck to start over.. because I am almost there...  all I need is the PHP code to create a little PHP code and turn $data[0] into the data from the csv file.

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.