insert into from 0-xxxx

Discussion in 'MySQL' started by Marty_, Sep 28, 2007.

  1. #1
    I need to insert information into for example "sample_table" as below it will be from Cat_ID 0 - 6388, how can I achieve this with out having to write lines 0-6388 ?

    
    INSERT INTO `sample_table` (`Cat_ID`, `Lang`, `Zone1`, `Zone2`, `Zone3`, `Zone4`, `Zone5`, `Zone6`, `Zone7`, `Zone8`, `Zone9`, `Zone10`, `Zone11`, `Zone12`, `Zone13`, `Zone14`, `Zone15`, `Zone16`, `Zone17`, `Zone18`, `Zone19`, `Zone20`) VALUES
    
    (0, 'English', 'some info', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''),
    
    
    (6388, 'English', 'some info', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
    Code (markup):

     
    Marty_, Sep 28, 2007 IP
  2. bluegrass special

    bluegrass special Peon

    Messages:
    790
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Insert into is to create new rows. Are you creating new rows or putting data into exotsing rows?

    If you are inserting new rows you can use a loop (either in SQL or in the page code). The syntax will vary depending on the DB you are using. Here is some psuedo code to demonstrate the principle:

    For i = 0 to 6388
    Insert Into table (cat_id, lang,...)
    Values (i, 'data',...)
    Next

    Of course, that only works if everything except the ID field is going to have the same data. Another method would be if you have the data in say a spreadsheet you can do an import. Also, if the Cat_ID is the unique identifier, it would be better form to not add its value through an insert, rather it would be better to set that filed to a self incrementing value.

    If you are adding data to existing rows, then you could do:

    Update table
    Set Lang = 'data'
    Where Cat_ID < 6389

    For more informative help:

    What DB and version are you using?
    Other than the ID will the data you want to add via this method be the same?
    Do you have this data already in a CSV or spreadsheet?
    Are you doing this through the database directly or through a php/asp page?
     
    bluegrass special, Sep 28, 2007 IP
  3. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply @bluegrass_special

    I'm creating new rows into an empty table, all but the Cat_ID starting at 0-6388 will be the same, (0-6388, 'English', 'same', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');

    I'm running (Windows Essentials (x86)) mysql 4.1.22.

    Preferable I would like to insert this information through SQL phpmyadmin or I'm open to suggestions for php 5.

    
    
    <?php
    
    For i = 0-6388; // which is wrong I know, so whats the correct way
    
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("database name") or die(mysql_error());
    
    mysql_query("INSERT INTO sample_table 
    (Cat_ID, Lang, Zone1) VALUES([B]What would go here, i ? [/B], 'English', 'data', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); ") 
    or die(mysql_error());  
     
    
    echo "Data Inserted!";
    
    ?>
    Code (markup):
     
    Marty_, Sep 29, 2007 IP
  4. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    I guess it would be something like this:

    
    <?php
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("database name") or die(mysql_error());
    for($count=0; $count<=6388; $count++){
        mysql_query("INSERT INTO sample_table
        (Cat_ID, Lang, Zone1,...put other columns here...)
        VALUES($count, 'English', 'data', '',     '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); ") 
        or die(mysql_error()); 
    }
    echo "Data Inserted!";
    ?>
    
    PHP:
    But if the Cat_ID is just an ID then you would probably want to have auto increment/identity on that coulmn, and leave out the Cat_ID column in the insert.

    If you already have the data in some spreadsheet or csv file, then you should check out phpMyAdmin import functions.
     
    kjewat, Sep 29, 2007 IP
  5. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks very much @kjewat however i'm getting the following error

    
    Parse error: syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in EDITED line 7
    PHP:
    7 been VALUES('$count, 'English', etc...

    Which I assume is something eles rather then the code ?
     
    Marty_, Sep 29, 2007 IP
  6. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Got that solved a small error in the code above, no harm done, it was my fault.

    @kjewat thanks a million
     
    Marty_, Sep 29, 2007 IP
  7. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #7
    No problem :)
     
    kjewat, Sep 29, 2007 IP
  8. janwei

    janwei Banned

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    youre welcome:D
     
    janwei, Dec 7, 2007 IP