2

I need some help to figure out how to add multiple fields, on a mysql db, from a textarea with multiple lines. I would like to have each line to be broken into 6 values (one for each field of my db).

for example, I have the following lines:

info1|info2|info3|info4|info5|info6

info1|infob|infoc|infod|infoe|infof

info1|info8|info9|info0|info1|info2

info1|info4|info4|info5|info6|info7

each field is separated with a "|" (thats because of the example I found online, will post here in a few.:)

then I have the following file: insert_form.php

<form action="insert_engine.php" method="post">
  <p>
    <textarea name="pctext" id="pctext" cols="100" rows="10"></textarea>
  </p>
  <p>
    <input type="submit" />
  </p>
</form> 

and I have the insert_engine.php:

<?php
$con = mysql_connect("localhost","USER","PASS");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("DBNAME", $con);

// assuming the text area value is in $_GET["pctext"]
$lines = explode("\n", $_GET["pctext"]);
foreach($lines as $line) {
  list($FIELD1, $FIELD2, $FIELD3, $FIELD4, $FIELD5, $FIELD6) = explode(" | ", $line);
    $sql="INSERT INTO TABLENAME (FIELD1, FIELD2, FIELD3, FIELD4, FIELD5, FIELD6)
      VALUES
      ('$_POST[FIELD1]', '$_POST[FIELD2]', '$_POST[FIELD3]','$_POST[FIELD4]', '$_POST[FIELD5]', '$_POST[FIELD6]')";

    if (!mysql_query($sql,$con))
    {
          die('Error: ' . mysql_error());
    }
    echo "record added";
}
mysql_close($con)
?> 
<meta http-equiv="refresh" content="2;URL=confirmation.php" />

To be really honest I barely know about php, I am learning, by force, looking at examples online, testing then on my Linux, etc. I got this one from another thread from here: Inserting text from textarea into MySQL database without losing formatting and I am trying to put it to work.

I understand that I am connecting to the MySQL, selecting the DB, getting the content from pctext (textarea), exploding each line and breaking by "|" (pipe) and then, using a loop (foreach), inserting into the TABLE.

When I click the submit button on the form, I go to the engine page and after 2 seconds i got to the confirmation page, nothing is inserted on my DB but I do have a empty registry, so something is going on.

Could anyone help me with this script?

thank you in advance

PC

Community
  • 1
  • 1
Paulo Capelo
  • 1,062
  • 1
  • 9
  • 13
  • If you barely know PHP, that's not a very good example to start with, you should start with simpler mysql interactions, as well as string and array function and manipulation. – Madara's Ghost Oct 15 '11 at 23:17
  • I would agree with you if wasn't for the fact that I need that. I already manage to add individual items, list, organizing by fields, etc. the next one is the one. :) – Paulo Capelo Oct 15 '11 at 23:33
  • 1
    [Little Bobby Tables](http://bobby-tables.com) is going to have a field day with your system... – Marc B Oct 15 '11 at 23:39
  • hey, I understand the implications, but this will be a internal system. but i did understand the risk. thank you anyway for the tip – Paulo Capelo Oct 16 '11 at 02:30
  • this is a good link from the page you sent: http://www.slideshare.net/billkarwin/sql-injection-myths-and-fallacies Thank you again – Paulo Capelo Oct 16 '11 at 02:41

2 Answers2

1
$lines = explode("\n", $_GET["pctext"]);

should be

$lines = explode("\n", $_POST["pctext"]);

Because your form method is set to 'post' and not 'get'

Also change

list($FIELD1, $FIELD2, $FIELD3, $FIELD4, $FIELD5, $FIELD6) = explode(" | ", $line);

to

list($FIELD1, $FIELD2, $FIELD3, $FIELD4, $FIELD5, $FIELD6) = explode("|", $line);

Because your example text doesn't have spaces around the |

Also.. list doesn't turn the elements into $_POST vars

('$_POST[FIELD1]', '$_POST[FIELD2]', '$_POST[FIELD3]','$_POST[FIELD4]', '$_POST[FIELD5]', '$_POST[FIELD6]')";

Should be

('$FIELD1', '$FIELD2', '$FIELD3','$FIELD4', '$FIELD5', '$FIELD6')";

Thilo Savage
  • 1,071
  • 11
  • 19
  • Thilo, Great, now I have all 4 line in the DB, but blank. I checked the DB and all 4 lines are there, again, blank. any other great advice???? :) Thank you again for pointing this issue. – Paulo Capelo Oct 15 '11 at 23:34
  • I did the correction got the same result: i got 4 new lines on my DB, but the lines are empty. thank ou again for your help :) – Paulo Capelo Oct 16 '11 at 00:03
  • Sorry, I forgot to read the second ajust. That was perfect. now I have data on the DB :) Thank you again Thilo :) – Paulo Capelo Oct 16 '11 at 00:07
0

Not only you're using $_GET instead of $_POST but you are missing language behaviour for constants too, array keys shoul be quoted, but in you case you using $_POST["FIELD1"] instead of $FIELD1

Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93