Explode TextArea to MYSQL

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 6785 Views - Last Post: 23 February 2011 - 03:42 PM Rate Topic: -----

#1 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Explode TextArea to MYSQL

Posted 23 February 2011 - 02:21 PM

Hey Guys, Im trying to have a textarea where the user will enter multiple emails. When they hit submit it will get put into my MYSQL DB but when I run my code, Only thing showin in my DB is 'email' any help is greatly appreciated.

<?php
include "opencon.php";
$emails = explode('\r\n', $_POST['list']);
$sql = mysql_query("INSERT INTO emails VALUES('id','email')");
foreach($emails as $email){
     $sql .= "('$email'),";
    }
  $sql = substr($sql,'',-1); //remove last character
  //echo "$sql";
  mysql_query($sql);
Print "Finished";
?>


and here is my form:
include("opencon.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View DB records</title>
</head>

<body>

<FORM ACTION="loademails.php" METHOD=POST>

One Email Per Line:<BR>
<TEXTAREA NAME="list" COLS=40 ROWS=6></TEXTAREA>

<P><INPUT TYPE=SUBMIT VALUE="submit">
</FORM>
<?php
$q = mysql_query("select * from emails");
if(mysql_num_rows($q)>0){
	$count = 1;
	while($record = mysql_fetch_array($q)){
	 echo $count." - ".$record["email"]."<br>";
	 $count++;
	}
}else{
	echo "No records in DB";
}
?>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Explode TextArea to MYSQL

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:26 PM

Your query is going to look something like this:

INSERT INTO emails VALUES('id','email')('[email protected]')('[email protected]')...etc.

Does that look right to you?
Was This Post Helpful? 0
  • +
  • -

#3 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:29 PM

Yes, but I have it all in a textarea and need to some how loop through ech one. Not sure how to do it.
Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:34 PM

Your DB is showing email because your settings the VALUES() to it, what your thinking is the column names I am assuming.

Try this instead,
$sql = mysql_query("INSERT INTO emails (email)
                    VALUES($emailList)");


In your loop append the emails to a variable, i used $emailList

This post has been edited by VolcomMky: 23 February 2011 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:43 PM

if I do t likethat using hte emaiList wouldnt that insert all emails into as 1 entry instead of multiple entries? Sorry for confusing...dont know a whole lot about php
Was This Post Helpful? 0
  • +
  • -

#6 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 170
  • View blog
  • Posts: 1,311
  • Joined: 22-October 08

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:44 PM

You would have to use a foreach loop, http://www.w3schools.com/ has some great articles on these loops. Say for example:
<?php
foreach($explode_var in $value)
{
mysql_query("INSERT INTO email_table('id','email') VALUES($value)");
}
?>


Something like this :)

Hope it helps
Was This Post Helpful? 0
  • +
  • -

#7 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:47 PM

Hey, got to thinking after you said that and it hit me, here is what I came up with:

$emails = explode('\r\n', $_POST['list']);
$sql = "INSERT INTO emails (email) VALUES ";
foreach($emails as $email){
     $sql .= "('$email'),";
    }
  $sql = substr($sql,'',-1); //remove last character
  //echo "$sql";
  mysql_query($sql);
Print "Finished";
?> 

Was This Post Helpful? 0
  • +
  • -

#8 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:52 PM

My above code now sticks all emails I put in the database on the same entry and not a seperate one. Hrmm
Was This Post Helpful? 0
  • +
  • -

#9 VolcomMky   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 02:59 PM

So run the SQL Query for every email found, so inside of the loop

Or, write multiple Queries in 1 and execute the one.

foreach($emails as $email){
     $sql = "INSERT INTO emails (email) VALUES ('$email')";
     mysql_query($sql);
}


Or

$sql = "";
foreach($emails as $email){
     $sql .= "INSERT INTO emails (email) VALUES ('$email');";
}
mysql_query($sql);

This post has been edited by VolcomMky: 23 February 2011 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:11 PM

the PDO guy speaking again … well, you can make such a foreach loop working well with prepared statements and not worry about whether you assemble the SQL string right:
# initialisation would come here …
$ps = $pdo->prepare("INSERT INTO emails SET `email` = ?");
$ps->bindParam(1, $email, PDO::PARAM_STR);
# use the forach loop
foreach ($emails as $email)
{
	$ps->execute();
}


@VolcomMky: this would require MySQL to allow for multiple queries, which is often disabled.

This post has been edited by Dormilich: 23 February 2011 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#11 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:14 PM

your code does the same as mine and puts all emails in the same entry
Was This Post Helpful? 0
  • +
  • -

#12 VolcomMky   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:19 PM

@Dormilich , sorry I dont know what is usually disabled or not because I usually build on in house servers.

@Twoods196 , which code did you try? neither of the codes I posted would put multiple emails on the same record.
Was This Post Helpful? 0
  • +
  • -

#13 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:21 PM

I tried both, here is the links, so you can see the results

removed

This post has been edited by Twoods196: 23 February 2011 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:24 PM

View PostVolcomMky, on 23 February 2011 - 11:19 PM, said:

@Dormilich , sorry I dont know what is usually disabled or not because I usually build on in house servers.

for public servers this is a common setting to prevent some SQL injection attacks.
Was This Post Helpful? 0
  • +
  • -

#15 Twoods196   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 21-April 09

Re: Explode TextArea to MYSQL

Posted 23 February 2011 - 03:26 PM

Dormilich, I have never used PDO stuff before...kinda looses me worse the it already does..:/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2