PHP/MySQL problems with textarea

software development

#1

Hi There-

I’m learning PHP and am trying to get a simple form to work (list all rows in table with each row name linked to a php?id=x edit data page). Everything seems to be working fine, but I’m having problems with the textarea input type. Here’s what I have, which won’t work (when I go to edit data, the field is null):

Content:

However, when I change this to it works fine (data to edit DOES display).

Any thoughts?

Also, I have a timestamp column that updates a new row with now() – in the format 20020804203835. What would be the best way in PHP to call this data to a page in the format “Sunday, August 4, 2002”? And is it possible (using PHP) to strip out the time to display in the format 8:38pm?

Thanks,
Gabe


#2

The code you show has no obvious faults. The fact that it works in an INPUT type=“text” is mysterious.

Some things to rule out:

Are you sure you have a terminating tag?

The only thing that could be different about the TEXTAREA is that the data might contain a new-line character (or two) that is messing you up somehow. Try using the TRIM() function on your data before displaying it.

In general, in the VALUE attribute of an INPUT field, you should use the STRIPSLASHES() function on the data, because when the data is posted to a form, single quotes are automatically escaped (i.e. ’ becomes '). (This is because the configuration switch “magic_quotes_gpc” is on.) This is good for data being posted to MySQL, but you have to take the quotes out before displaying them in a form.

So, try a tag that looks like this and see if it makes any difference:

<TEXTAREA NAME=“content” …><?php echo STRIPSLASHES(TRIM($content))?>

Finally on this subject, there’s a shorthand for <?php echo $variable ?>. You can do this: <?= $variable ?>.

On the date questions, you can do all the formatting you need as the data comes out of the database. See the MySQL DATE_FORMAT function. Your SELECT statement might look like:

SELECT *, DATE_FORMAT(date_created, ‘%e/%c/%y’) AS date_short

Then, you can refer to the preformatted date in PHP code as if there was a field in the database called ‘date_short’.

Failing that, see the PHP date() function.

–David B.
“greendavid”


#3

Whoops! Mis-read your code. There is an obvious problem.

The TEXTAREA tag does not use the VALUE attribute. Instead, enclose the value between the start and end tags, like this:

< TEXTAREA NAME=“content” …><?= $content ?></ TEXTAREA>

That’ll do it.

Also, here are the addresses of the docs:

PHP: http://www.php.net
MySQL: http://www.mysql.com/doc/en/index.html

–David B.
“greendavid”


#4

Thanks for the input, David. I did confirm the close tag’s presence. I tried your stripslashes suggestion:

Content:<textarea name=“content” wrap=“virtual” rows=“10” cols=“40"
value=”<?php echo STRIPSLASHES(TRIM($content))?>

…but still no luck. Although the problem is a bit different now. For some reason, it seems like part of the HTML code is being associated as part of my $content variable. I don’t see any errors elsewhere in the code (most of which I copied from this tutorial anyway:http://hotwired.lycos.com/webmonkey/99/21/index3a_page6.html?tw=programming). Now when I go to edit a record, here’s what I see in the $content field:

Weird, eh?

Thanks also for input on calling date display. I’ll give that a shot once I get past this hurdle!

-Gabe


#5

Woohoo! That did it! Thanks again, David.