Saving content from a textarea box into table as seperate records ?

Discussion in 'PHP' started by bluearrow, Aug 11, 2010.

  1. #1
    I have a site list which I want to save it to a mysql DB. I get these sites as a list of 30-60 sites so I don't want to do it line by line.

    So i want to paste this list into a textarea box and then create a record in a table for each of these sites. :(

    can someone tell me how to do this with PHP ?
     
    bluearrow, Aug 11, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    This would be the bare minimum

    
    <?php
    
    require('includes/config.php'); // method to connet to the database
    
    $links_list  = $_POST['links'];
    
    $arr = explode("\n", $links_list);
    
    foreach($arr as $link){ 
    mysql_query("INSERT INTO `table` VALUES('$link')") or die(mysql_error());  
    }
    
    ?>
    
    <p>Your Links<br />
    <form action="#" method="post">
    <textarea name="links" rows="4" cols="20"></textarea>
    <p><input type="submit" value="GO">
    </FORM> </p>
    
    PHP:
    You would probable want to make some checks to see if a link already exist in the database, so maybe adding an id to table would help with that.
     
    Last edited: Aug 11, 2010
    MyVodaFone, Aug 11, 2010 IP
    bluearrow likes this.
  3. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When pasting the sites in the textarea box use a pattern for example; site1.com;site2.com;site3.com;site4.com;site5.com In this example we have separated the sites using the ; When you submit the form to the server use the string function explode () which will return an array of strings from a string based on the separator. This is the prototype:

    array explode ( string $delimiter , string $string [, int $limit ] );

    Then save each array variable in the mysql database. Do you have the basic knowledge in PHP & Mysql/
     
    caciocode, Aug 11, 2010 IP
    bluearrow likes this.
  4. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Thank you guys. Got it sorted out using explode function ! :)
     
    bluearrow, Aug 11, 2010 IP