Forum Moderators: coopster

Message Too Old, No Replies

send multiple form data to mysql

multiple form data using php for mysql data entry

         

twistedperv

10:42 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



hey guys! Im doing my best at learning php but here lately Ive come across something I have no idea how to do.

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.

ironik

10:56 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



After you submit your text field you can split it up by newline characters (in the example I've converted the newlines to breaks just to make it simpler):


$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.

twistedperv

12:19 am on Apr 20, 2005 (gmt 0)

10+ Year Member



man, Im going to email you a big kiss! lol, right after I kick my own butt for not realizing that.

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>