PHPNewbie55 Posted December 21, 2007 Share Posted December 21, 2007 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!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/ Share on other sites More sharing options...
btherl Posted December 21, 2007 Share Posted December 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-420299 Share on other sites More sharing options...
BenInBlack Posted December 21, 2007 Share Posted December 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-420303 Share on other sites More sharing options...
BenInBlack Posted December 21, 2007 Share Posted December 21, 2007 humm, btherl I wondered that when i posted my first response then in that case he would have to make sure $data was setup in 2nd file like the first. Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-420305 Share on other sites More sharing options...
PHPNewbie55 Posted December 21, 2007 Author Share Posted December 21, 2007 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.......!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-420667 Share on other sites More sharing options...
PHPNewbie55 Posted December 21, 2007 Author Share Posted December 21, 2007 Bump...... Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-420804 Share on other sites More sharing options...
PHPNewbie55 Posted December 22, 2007 Author Share Posted December 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82636-solved-problem-passing-variables-part2/#findComment-421095 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.