Current location: Hot Scripts Forums » Programming Languages » PHP » Problem when inserting values in database.

Problem when inserting values in database.

Reply
  #1  
Old 06-29-09, 06:18 PM
Gaug Gaug is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Problem when inserting values in database.

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($handle4096',')) !== 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:
NoticeUndefined variablekey in site on line 41

Notice
Undefined offset1 in site on line 42

Notice
Undefined offset2 in site on line 42

Notice
Undefined offset3 in site on line 42

Notice
Undefined offset4 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.
Reply With Quote
  #2  
Old 07-01-09, 10:43 PM
windows7 windows7 is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Try this
PHP Code:
$import="INSERT into main_table(key,name,id,last name) values('$key','$data[1]',$data[2],'$data[3]')"
remove ' ' at $data[2]
Reply With Quote
  #3  
Old 07-02-09, 09:51 AM
Gaug Gaug is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
It's almost complete here:

PHP Code:
<?php
require_once('Connections/conex.php');
mysql_select_db($database_conex$conex);
if(isset(
$_POST['submit']))
{
 if (
move_uploaded_file($_FILES['filename']['tmp_name'], "file.csv"))
 {
  echo 
"File sucessfully uploaded."."<BR>";
  
$filename "test.csv";
  
$handle fopen($filename'r');
  }
 else
 {
  echo 
"Error : " $uploaddir basename($_FILES['filename']['name'])  . '\n' $_FILES['filename']['error'] . "<BR>";
  }
 
$data2='S';
 
$sw=0;
 while ((
$data fgetcsv($handle4096',')) !== FALSE)
 {
  
$data str_replace("'","''",$data);
  
$prueba $data[0]; /// M
  
if(!empty($sw))
  {
   if(
$prueba!=$data2)
   {
    
$sintaxis=$prueba."%";
    
$query_Recordset "SELECT * from table2 WHERE complete_key like '".$sintaxis."' order by complete_key DESC limit 0,1";
    
$Recordset mysql_query($query_Recordset$conex) or die(mysql_error());
    
$row_Recordset mysql_fetch_assoc($Recordset);
    
$complete_key $row_Recordset['complete_key'];
    
$key=explode($prueba,$complete_key);
    
$key[1]++;
    
$key $prueba.$key[1];
    
$import="INSERT INTO main_table (key1,name,id,last_name) values('$key','$data[1]','$data[2]','$data[3]')";
    
$import2="UPDATE table2 SET complete_key = '$key' WHERE complete_key = '$complete_key'";
    
$runq mysql_query($import) or die(mysql_error());
    
$runq2 mysql_query($import2) or die(mysql_error());
    }
   }
  
$sw=1;
  }
 
fclose($handle);
 print 
"Import done";
 }
else
{
 echo 
'<form action="#" method="post" enctype="multipart/form-data">
          Select file to import:<br>
          <input type="file" name="filename" size="20"><br>
          <input type="submit" name="submit" value="submit">
          </form>'
;
 }
//$import="select * from main_table";
//$runq = mysql_query($import) or die(mysql_error());
//$import2="select * from table2";
//$runq2 = mysql_query($import2) or die(mysql_error());
//DbClose();
?>
And shows up this errors:

PHP Code:
NoticeUndefined offset1 in site on line 42

You have an error in your SQL syntax
check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 1 
This is line 42:
PHP Code:
     $key[1]++; 
Greetings.
Reply With Quote
  #4  
Old 07-02-09, 08:18 PM
Gaug Gaug is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Problem solved, Thanks.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bottom inserting into database? how can i do this?? hossamhamdi1986 JavaScript 7 08-12-06 10:11 AM
Problem with inserting values into an access db pinochio53 ASP 1 03-29-05 01:53 PM
i have a problem regarding updating database online GENIUSAdnan PHP 1 06-23-04 04:53 AM
form not inserting information into the database sabret00the PHP 7 12-01-03 04:35 PM
asp: values in array not in order?? seala ASP 0 08-16-03 01:06 PM


All times are GMT -5. The time now is 11:28 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)