Jump to content


Photo

Data won't be saved onto mysql database


  • Please log in to reply
4 replies to this topic

#1 bertieboy_93

bertieboy_93
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 17 September 2006 - 08:29 PM

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:
<?
/*
$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.";
?>

Thanks in advance for any help.

#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 08:32 PM

Try adding

or die(mysql_error());
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

#3 bertieboy_93

bertieboy_93
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 17 September 2006 - 08:36 PM

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?

#4 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 08:43 PM

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);

#5 bertieboy_93

bertieboy_93
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 19 September 2006 - 07:42 PM

Here is my new update script.

<?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());
?>

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:
http://www.phpfreaks...c,108657.0.html




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users