Reputation: 1092
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
Upvotes: 2
Views: 2825
Reputation: 25918
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
Upvotes: 0
Reputation: 1071
$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')";
Upvotes: 1