Hello, this is my first post in this forum, eventhough my english is not perfect, I'll to explain my problem the best I can, if you understand anything about what I say, just tell me.
I got a code that takes values from a CSV file (Coma Delimited - Excel), something like this:
PHP Code:
key name id last name
J juan 87 perez
J pedro 75 rodriguez
B mario 98 gonzalez
I want to upload these values and place them in a table in the database, this isn't the problem, the problem is that, for example, in 'key' i got a "J" in the first row, I want to link in someway this letter, with other values that I have in ANOTHER table in the same database, the value would be something like "J12345", but there's also more values, let's suppose the table is something like this:
PHP Code:
Table: 'table2'
complete key (Fields)
J2485 (Values)
B8789
J1589
B8796
Now, in the first row of the CSV file that I'm uploading, the key is 'J', it should just take into account the ones that start with the letter 'J', and take the bigger one, in this case would be 'J2485', then I want to add '+1' to this value and insert it in the 'main table' where I'm going to upload everything, the same if changes from 'J' to 'B' or viceversa and I'd want something like this:
PHP Code:
Table: 'main_table"'
key name id last name
J2486 juan 87 perez
J2487 pedro 75 rodriguez
B8797 mario 98 gonzalez
Here is a piece of the code that does this, it's almost complete, but it still shows up some errors:
PHP Code:
$data2=0;
while (($data = fgetcsv($handle, 4096, ',')) !== FALSE) {
$data = str_replace("'","''",$data);
if($data[0]!=$data2){
$sintaxis=$data[0]."%";
$query_Recordset = "SELECT * from table2 WHERE complete_key like '".$sintaxis."' order by complete_key ASC limit 0,1";
$Recordset = mysql_query($query_Recordset, $conex) or die(mysql_error());
$row_Recordset = mysql_fetch_assoc($Recordset);
$clave=$row_Recordset['llave'];
$data2=$data[0];
}
$clave++;
$import="INSERT into main_table(key,name,id,last_name) values('$key','$data[1]','$data[2]','$data[3]')";
$runq = mysql_query($import) or die(mysql_error());
echo $import;
echo "<br>";
}
And it shows up these errors:
PHP Code:
Notice: Undefined variable: key in site on line 41
Notice: Undefined offset: 1 in site on line 42
Notice: Undefined offset: 2 in site on line 42
Notice: Undefined offset: 3 in site on line 42
Notice: Undefined offset: 4 in site on line 42
This is the line 42:
PHP Code:
$import="INSERT into main_table(key,name,id,last name) values('$key','$data[1]','$data[2]','$data[3]')";
I really hope you can help me with this, I just can't find an answer to this problem, thanks in advance.
Greetings.