Jump to content

Write each textarea line as a new row in database


perky416

Recommended Posts

I have tried using the code below which i found in another post however nothing writes to the database.

 

if ($_POST['submit'])
{
$values = $_POST['textarea_name'];
$array = split("\r\n", $values);
foreach($array as $line)
{
	mysql_query("INSERT INTO table_name('field_name') VALUES($line)");
}
}

Field identifiers are not strings (are not surrounded by quotes) in SQL.

 

You should also enable error reporting when developing and always attempt to catch errors in your code, you have done nothing to check if your query succeeded or to indicate why it has failed.

split

From manual> Warning

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

 

 

Doesn't mean it won't work. It's as thorpe stated, quoting the wrong value and no error catching.

 

if (isset($_POST['submit'])) {
    $lines = split("\r\n", $_POST['textarea_name']);
    if(!empty($lines)) {
        foreach($lines as $line) {
            mysql_query("INSERT INTO table_name(field_name) VALUES('".mysql_real_escape_string($line)."')") or trigger_error('MySQL: '.mysql_error(), E_USER_ERROR);
        }
    }
}

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.