Text Box Rows to MySQL Rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Text Box Rows to MySQL Rows

Post 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?) :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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&#1111;'textarea']);
foreach ($temp as $saveval) &#123;
  // write $saveval to db;
&#125;
should do the job.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$lines = preg_split('#\r\n|\n\r|\r|\n#', $textData, -1 PREG_SPLIT_NO_EMPTY);
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post 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... :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

or.... you can upload a CSV file.. or some other format you can build/generate/whatever...
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post 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&#1111;'data']); 
  foreach ($temp as $saveval) &#123; 
  $clean = trim($saveval);
  $info = explode(",", $clean);
  $query = "INSERT INTO `table` (`name`, `email`, `phone`, `address`) VALUES ( ". $info&#1111;0] . " , " . $info&#1111;1] . " , " . $info&#1111;2] . " , " . info&#1111;3] .")";
  $result = mysql_query($query) or die("error: " . mysql_error());
  
   &#125;
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.. :)
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post 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&#1111;'data']); 
  foreach ($temp as $saveval) &#123; 
  $clean = trim($saveval);
  $info = explode(",", $clean);
  $unique = $info&#1111;0];
  $name = $info&#1111;1];
  $phone = $info&#1111;2];
  $address = $info&#1111;3];
  $query = "INSERT INTO `cc` (`unique`, `name`, `phone`, `address`) VALUES ( "$unique" , "$name" , "$phone" , "$address")";
  $result = mysql_query($query) or die("error: " . mysql_error());
  
   &#125;
Thanks, but if there is a better way for me to do this.. please let me know. Thanks for the help guys.
Post Reply