Jump to content

Inserting an array of lines into an SQL table


_DarkLink_

Recommended Posts

Hello everyone and I would like you guys to have a look at this piece of code.

 

<?php
if(isset($_POST['add'])) {

   $self = $_SERVER['PHP_SELF']; //the $self variable equals this file
   $ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP

   //connect
   $connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
   mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

   //fetch data
   $data = htmlspecialchars($_POST['list']);
   $comment =  mysql_real_escape_string($_POST['comment']);

   $data_lines = explode( "\r\n", $data );
   $comment_lines = explode( "\r\n", $comment );
   for($i=0;$i<count($data_lines);$i++)
   {
      $data_fields = explode( ",", $data_lines[$i]);

      $time = time();
      $queryb = "INSERT INTO coords SET tag='$data_fields[0]', guild='$data_fields[1]', name='$data_fields[2]', base='$data_fields[3]', econ='$data_fields[5]', maxecon='$data_fields[6]', location='$data_fields[4]', comment='$comment_lines[$i]', ipaddress='$ipaddress' ,date='$time';";

      // if it succeeds, display message
      if (@mysql_query($queryb))
      {
         echo('<p class="success">Successful posting of ['.$data_fields[3].']!</p>');
      }
      else
      {
         echo('<p class="error">Error could not post ['.$data_fields[3].'] to database!</p>');
      }
   }//end for loop
}//end if $_POST['add'] statement
?>

 

As you can see, it gets data received from a form's submission and explodes them into lines.

 

For $data, it explodes it again for each comma.

It then inserts everything for each comma, in a new column and for each line, on a new row.

 

Now for $comment, it should be inserting it on a new row for each line, which isn't doing it.

Been looking and testing at it for a few days now.

 

The comment column is a varchar of 100 of length.

 

So basically, it does everything i need it to do except inserting $comment on a new row for every line.

 

Any help is appreciated.

Thank you in advance.

What I am trying to do here is:

The user inputs in two text areas,

 

the first one is named "list" and the user inputs things like so:

field1,field2,field3...

field1,field2,field3...

 

and it keeps going on until the user is satisfied of the number of rows of data added into the table.

 

The second one is named "comment" and the user inputs a line of comment for each of the rows that were inserted using the first text area.

 

The rows in the table are then fetched on the page that I need it in the form of a table. But that is already done and works perfectly.

 

Hope I explained it thoroughly,

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.