SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting into 2 tables correct syntax?

    Hi All

    I am inserting data from a CSV file into a table in mysql. It works fine but I have now had to split the table into 2 tables.

    I need to insert some of the data into the first table then the rest into the second table and loop through untill all of the rows have been completed.

    Here are my PHP statements

    Original statement:

    PHP Code:
      while (($data fgetcsv($handle3000"|")) !== FALSE)

             {

            
            
    mysql_select_db($database_tringAucconn$tringAucconn);
            
    $import="INSERT INTO aItems (aItemCalId,aItemaId,aItemCatId,aItemLotNo,aItemSDesc,aItemLDesc,aItemEst,aItemHam) values('$calId','" mysql_real_escape_string($data[0]) . "','" mysql_real_escape_string($data[2]) . "','" mysql_real_escape_string($data[3]) . "','" mysql_real_escape_string($data[4]) . "','" mysql_real_escape_string($data[5]) . "','" mysql_real_escape_string($data[6]) . "','" mysql_real_escape_string($data[8]) . "')";
            
    mysql_query($import) or die(mysql_error());

         
    fclose($handle); 
    New query:

    PHP Code:
      while (($data fgetcsv($handle3000"|")) !== FALSE)

             {

            
            
    mysql_select_db($database_tringAucconn$tringAucconn);
            
    $import="INSERT INTO aItems (aItemCalId,aItemaId,aItemCatId,aItemLotNo,aItemSDesc,aItemLDesc,aItemEst,aItemHam) values('$calId','" mysql_real_escape_string($data[0]) . "','" mysql_real_escape_string($data[2]) . "','" mysql_real_escape_string($data[3]) . "','" mysql_real_escape_string($data[4]) . "','" mysql_real_escape_string($data[5]) . "','" mysql_real_escape_string($data[6]) . "','" mysql_real_escape_string($data[8]) . "')";
            
    mysql_query($import) or die(mysql_error());

            
    $ims="INSERT INTO aImages (aImagesCalId,aImagesLotNo,aImagesThumb,aImagesLarge) values('$calId','" mysql_real_escape_string($data[3]) . "','default.png','default.png')";
            
    mysql_query($ims) or die(mysql_error());

             }

         
    fclose($handle); 
    Can anyone confirm if this is right before I deploy this to the live database?

  2. #2
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi mickthetree,

    that looks mostly right. Im not sure you have to keep selecting the database in the loop...but if your original code worked so will the new code...as in, it will insert stuff into 2 different tables

    Why dont you just try it on a test server?

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are inserting data to two separate tables in same database then you dont need that select_db inside the loop because you never change database in the process. It will be just useless query every looping round.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •