Hey!

I want to store the contents of a textarea into a database, but I want to preserve the line breaks so that when the data is put back out the breaks are there.

Without nl2br() my script runs normally, but with nl2br() I get the following mysql error.

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(year,make,model,trim,exterior,interior,price,vin,description) VALUES ('2013','F' at line 1"

The value that I want to preserve the breaks for is description.

Thanks!
-Chase

    What's the actual code? Text should go unformatted to database and when you fetch and print rows, then use nl2br function. You probably haven't escaped the contents from your form. To prevent this, use PDO or Mysqli instead of Mysql(which you really should not be using anymore).

    Anyway, insert should go like this(using PDO):

    $sql = "INSERT INTO table (year,make,model,trim,exterior,interior,price,vin,description) VALUES (:year,:make,:model,:trim,:exterior,:interior,:price,:vin,:description)";
    $q = $conn->prepare($sql);
    $q->execute(
    	array(
    		':year'			=> $year,
    		':make'			=> $make,
    		':model'		   => $model,
    		':trim'			=> $trim,
    		':exterior'		=> $exterior,
    		':interior'		=> $interior,
    		':price'		   => $price,
    		':vin'			 => $vin,
    		':description'	 => $description
    	)
    );
    

    Ofcourse those $year, $make etc. comes from your form so change them accordingly to $POST['year'], $POST['make'] and so on..

      Can you show us how you're using it?

      Also, you most likely shouldn't be using nl2br() on the data when storing it in the DB. Store the raw line break characters just as they are. If you want them to be converted into HTML entities when you later retrieve that data, then do so at that point - not before.

      EDIT: Oops, too slow.

        Write a Reply...