Jump to content

Storeing looped array data frm php to MySQL


UpcomingPhpDev

Recommended Posts

Hi again phpfreaks.

 

I have a slight problem that I have been testing through trial and error but I cant seem to get it, Ive tried a few searches with google too but the results are differnt to what im trying to achieve.

 

Basically I have a textarea field which ive stored in a certain variable and have passed it to the nl2br function.

Then I have used a foreach loop to echo each single line out whilst adding a few characters before and after each line.

The problem im having is when I pass the variable to MySQL to store it, it only saves the last line on the textarea variable.

 

Here is my code.(I havnt included all such as the textarea,connection to db, selected db etc)

 

$BrokenLines = $_POST['brokenlines'];
$BrokenLines = nl2br($BrokenLines);
if(!empty($BrokenLines))
{		
	echo "<div id='array'>";
		$Array = explode("<br />", $BrokenLines);
		foreach($Array as $Key => $Value)
			{
				$SQLBrokenLines = "[9s28d\"$Value\"]".$Value."[d82s9]<br />";
				echo $SQLNormalLinks;
			}
      	echo "</div>";         
}

$Sql = "INSERT INTO TextArea (BrokenLines) VALUES
		       ('$BrokenLines')";

 

The problem im having is that it will only save the last line from the textarea into the mysql table.

Thanks in advance guys. Il continue to test some stuff myself.

I don't see $brokenlines being what u need for the query.

 

 

You need a comma separated list for the query as written.

 

Sorry, I renamed 1 of the variables so you guys could read the script easier and forgot to change that part.

 

It should be

 

$BrokenLines = $_POST['brokenlines'];
$BrokenLines = nl2br($BrokenLines);
if(!empty($BrokenLines))
{		
	echo "<div id='array'>";
		$Array = explode("<br />", $BrokenLines);
		foreach($Array as $Key => $Value)
			{
				$SQLBrokenLines = "[9s28d\"$Value\"]".$Value."[d82s9]<br />";
				echo $SQLBrokenLines;
			}
      	echo "</div>";         
}

$Sql = "INSERT INTO TextArea (BrokenLines) VALUES
		       ('$SQLBrokenLines')";

Change this line:

$SQLBrokenLines = "[9s28d\"$Value\"]".$Value."[d82s9]<br />";

 

To this:

$SQLBrokenLines .= "[9s28d\"$Value\"]".$Value."[d82s9]<br />";

 

That way it'll add the next line on and the next line and so on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.