Page 1 of 1
Text Box Rows to MySQL Rows
Posted: Mon Feb 28, 2005 10:37 am
by alix
hello all,
Bet you're getting tired of my dumb questions... im trying though.
I searched the forum for something like this but i didnt find anything very usefull and if i did come across something i prolly didnt know waht i was looking at..
here's what im trying to do... Im trying to make a text box that i can have someone put data into, in a delimited format, (1 line = 1 record(row)... <tab> delimited) hit submit and have the data inserted into the database.
for example this is my form:
Code: Select all
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="proccess.php">
<p>
<textarea name="data" cols="100" rows="25"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
how to go about breaking the data up into seperate rows is what i dont know how to go about doing. -Thanks guys/(girls?)

Posted: Mon Feb 28, 2005 10:44 am
by CoderGoblin
look up the command
http://de3.php.net/manual/en/function.explode.php and/or split in the manual.
The new line is in all likelyhood either a "\n" or "\r" so:
Code: Select all
$temp=explode("\n",$_POSTї'textarea']);
foreach ($temp as $saveval) {
// write $saveval to db;
}
should do the job.
Posted: Mon Feb 28, 2005 10:45 am
by feyd
Code: Select all
$lines = preg_split('#\r\n|\n\r|\r|\n#', $textData, -1 PREG_SPLIT_NO_EMPTY);
Posted: Mon Feb 28, 2005 11:46 am
by AGISB
use the WRAP="HARD" in the textarea tag. This will place \n in each line. This will also place those into the database.
Posted: Mon Feb 28, 2005 12:37 pm
by alix
alright i see how that makes each line from the texbox its own row... now how do i get it to reconize the delimiter so that the information will go into the correct field?
say my delimiter is a comma I want to be able to put this type of data into the text box:
name, address, email, phone
name2, address2, email2, phone2
Each line is its own row with the data in the correct Column.
sorry if this question seems elementary or its already been answerd... eventually the questions will slow...

Posted: Mon Feb 28, 2005 12:47 pm
by feyd
explode() as linked to by CoderGoblin's post will break them apart.. you then put them back together in whatever manor you deem necessary for the expected input. Then insert the data. Wash, rinse, repeat.
Posted: Mon Feb 28, 2005 12:48 pm
by AGISB
I would have to ask why?
Why not make a form with multiple simple text fields? Using one text box is just asking for problems. Even if only you uses this form you will always be forced to use the correct syntax for delimination. Validation also is a pain in the ass.
Posted: Mon Feb 28, 2005 1:07 pm
by alix
I have to come up with a way that large amounts of data can be insterted into the database via the web... Right now the only method i have is to create a text file and use the load feature on phpmyadmin which is a pain and only I can do it... its either this method where a comma delimited file can be copied and pasted into the text box by someone else or a way to have a place where you just browse for the file and just hit submit like phpmyadmin has.
Posted: Mon Feb 28, 2005 1:08 pm
by feyd
or.... you can upload a CSV file.. or some other format you can build/generate/whatever...
Posted: Mon Feb 28, 2005 1:23 pm
by alix
i have this so far... does it look like im on the right track by any means??
Code: Select all
include ("db.php");
$temp = explode("\n",$_POSTї'data']);
foreach ($temp as $saveval) {
$clean = trim($saveval);
$info = explode(",", $clean);
$query = "INSERT INTO `table` (`name`, `email`, `phone`, `address`) VALUES ( ". $infoї0] . " , " . $infoї1] . " , " . $infoї2] . " , " . infoї3] .")";
$result = mysql_query($query) or die("error: " . mysql_error());
}
Feyd, that would be a great thing to be able to do... just not sure on the means to do it... Hopefully i'll begin to put all these methods together soon so that everything i do wont be something new to learn... it all takes time. so bear with me..

Posted: Mon Feb 28, 2005 2:17 pm
by alix
lol, i need to test the code before i post it.. this seemed to work actually..
Code: Select all
include ("db.php");
$temp = explode("\n",$_POSTї'data']);
foreach ($temp as $saveval) {
$clean = trim($saveval);
$info = explode(",", $clean);
$unique = $infoї0];
$name = $infoї1];
$phone = $infoї2];
$address = $infoї3];
$query = "INSERT INTO `cc` (`unique`, `name`, `phone`, `address`) VALUES ( "$unique" , "$name" , "$phone" , "$address")";
$result = mysql_query($query) or die("error: " . mysql_error());
}
Thanks, but if there is a better way for me to do this.. please let me know. Thanks for the help guys.