Forum Moderators: coopster
Im trying to use a textarea form field to input oh say 1000 entries to the mysql database. Each new line in the form field constitutes and new data entry and each table field would be delimited with a , or a ¦.
I know I need to have php grab each line and throw it into and array for processing to the db, but like I said, Im too new to get the gist of where I need to head to.
Ive searched and searched til Im blue in the face and have yet to come up with a solution. If anybody has a link to a tut or a code snippet or anything I could look at to base mine off of it would be greatly appreciated.
$text = nl2br($text);
$lines = explode('<br />', $text);
foreach ($lines as $line)
{
$fields = explode(',', $line);
$field_one = trim($fields[0]);
$field_two = trim($fields[1]);
}
That will allow you to break up the response from the text field, split it into lines and then split those lines into fields seperated by a comma.
The little line you gave me, after plugging it into what I had, worked like a champ! It split, inserted and put it all into the right fields in the table!
Here's my final code just incase anyone needs it. If it looks like a poor example of a coding job, then duh!, Im still learning.... :)
<?
require('includes/functions.php');
include ('includes/header.php');
$form_action="".$_SERVER["PHP_SELF"]."?func=submit";
$func=$_GET["func"];
?>
<html>
<head>
<title>Text Area to Mysql, Multiple Entries</title>
</head><body>
<?if( $func == "submit" ) {
$textarea = nl2br($textarea);
$lines = explode('<br />', $textarea);
foreach ($lines as $line)
{
$fields = explode(',', $line);
$gal_url = trim($fields[0]);
$gal_cat_id = trim($fields[1]);
$gal_sub_cat_id = trim($fields[2]);
$gal_description = trim($fields[3]);
$gal_site_id = trim($fields[4]);
$gal_url_mask = trim($fields[5]);
$sql=mysql_query("insert into gallery (gal_url,gal_cat_id,gal_sub_cat_id,gal_description,gal_site_id,gal_url_mask) values ('$gal_url','$gal_cat_id','$gal_sub_cat_id','$gal_description','$gal_site_id','$gal_url_mask')") or die(mysql_error());
}
echo "All entries entered correctly!\n";
$result = mysql_query($sql);
} else {
?>
<strong>
<font color="#000000" size="2" face="Geneva, Arial, Helvetica, sans-serif">
url, cat id, sub cat id, description, site id, fake url
</font>
</strong><form action="<?php echo $form_action;?>" method="post" id="form">
<textarea name="textarea" cols="120"></textarea>
<br>
<br>
<input type="submit" name="Submit" value="submit">
</form><?
}
include ('includes/footer.php');
?>
</body>
</html>