www.webdeveloper.com
Results 1 to 3 of 3

Thread: Split insert textarea value per line and insert each line as one record

  1. #1
    Join Date
    Sep 2013
    Posts
    221

    Split insert textarea value per line and insert each line as one record

    I need to know how to do the following using php coding please

    1.take inserted text from a textarea
    2.split the content per line
    3.Insert each line into the database as its own row with own id number
    Can anyone show me how to do this please?

    THis is what I have tried so Far:

    Below is my form page display_form.php:
    HTML Code:
    <form method="post" action="display_db.php">
    Member ID:
          <input name="mid" type="text" value=""><br/><br/>
    Enter Keywords:
          <input name="keywords" type="text" value=""><br/><br/>
          Enter Your Links :
    <textarea name="links" rows="3" cols="20"></textarea><br/><br/>
    
    
    
    <input type="submit" id="button" value=" Submit "/>
    
    </form>
    And below is my display_db.php:
    PHP Code:
    <?php
    $con
    =mysqli_connect("localhost","root","","test");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }
      
      if(isset(
    $_POST["data"]))
    {
      
    $line_data explode("\n"$_POST["keywords"]);
      foreach(
    $line_data as $key => $value)
    {
      
    $sql "INSERT INTO table (display) VALUE('{$value}')";
     
    mysql_query($sql);
    }
    }
      if (!
    mysqli_query($con,$sql))
      {
      die(
    'Error: ' mysqli_error($con));
      }
    echo 
    "1 record added";

    mysqli_close($con);
    ?>
    Ant kind of help is appreciated.
    Last edited by NogDog; 12-14-2013 at 01:17 PM. Reason: added [php] tags

  2. #2
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    5
    HI, if you want to get every line in the textarea you have to USE this:
    [PHP]
    f(isset($_POST['data']))
    {
    $line_data = explode("\n", $_POST['links']);
    }

    add name to submit:
    PHP Code:
    <input type="submit" id="button" value=" Submit " name="data"/> 

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,530
    I'm a bit confused, as your textarea is named "links", which you don't reference in your code, while you try to explode on "keywords", which is a text-type input field, which should not allow newlines. Assuming you really want to do the exploding and inserting of the "links" data:
    PHP Code:

    $data 
    preg_split('\s*[\r\n]+\s*#'trim($_POST['links']));
    $values = array();
    foreach(
    $data as $val) {
        
    $values[] = "('".mysqli_real_escape_string($con$val)."')";
    }
    $sql "INSERT INTO table (display) VALUE
    "
    .implode(",\n"$values); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles