Jump to content

Archived

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

bertieboy_93

Data won't be saved onto mysql database

Recommended Posts

Hi, I'm not sure if this is a php problem or mysql problem, so sorry if this is in the wrong forum.

I have a form which is used for a cms. You enter text into the form, it sends the data to a confirmation page just so you can check for any typos etc. Then it stores the data from the form into a session and passes it onto the update script when the user presses OK. Sometimes this all works great, and the entry in the database. Other times, it doesn't update. Everything is passed onto the confirmation dialogue fine, so it can't be a problem with my form. What I'm puzzled about is why it isn't always updating the database entry correctly. A lot of the time this happens when pasting from notepad, but I can't see why this would be a problem.

My update script:
[code]
<?
/*
$pagename is a way of identifying which record to update in the database.
$pageheading is the pageheading data retrieved from a session variable from the confirmation page.
$pagetext is the pagetext data retrieved from a session variable from the confirmation page.
PageHeading is varchar
PageText is mediumtext
*/
//Update database
$query = "UPDATE Main SET PageHeading = '$pageheading', PageText = '$pagetext' WHERE PageName = '$pagename'";
mysql_query($query);
echo "Your changes on the $pagename page have been saved.";
?>[/code]

Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Try adding

[code] or die(mysql_error());[/code]
to the end of your mysql_query line.  That way if there's an error it might give a hint as to what went wrong

Share this post


Link to post
Share on other sites
Wow! Thanks for responding so quickly. I did that and have determine that the problem is about apostrophes in the text field. Is there any way of stopping mysql getting upset about apostrophes?

Share this post


Link to post
Share on other sites
Use mysql_real_escape_string on the input and it should take care of it.

If that makes it start printing extra slashes when you print the input from the DB later, use stripslashes() before you print it. 

For example

$string = mysql_real_escape_string($string);

or when you print it, $string = stripslashes($string);

Share this post


Link to post
Share on other sites
Here is my new update script.

[code]
<?php
//Update database
$query = "UPDATE Main SET PageHeading = '$pageheading', PageText = '$pagetext' WHERE PageName = '$pagename'";
$query = mysql_real_escape_string($query);
$query = stripslashes($query);
mysql_query($query) or die(mysql_error());
?>
[/code]

The trouble is, at the beggining of each new line the letters rn are displayed. I don't know why this is happening.

EDIT: Someone seems to be having a similar problem here:
[url=http://www.phpfreaks.com/forums/index.php/topic,108657.0.html]http://www.phpfreaks.com/forums/index.php/topic,108657.0.html[/url]

Share this post


Link to post
Share on other sites

×

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.