473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting Messed Up with Single Quotes

155 100+
PHP Version 5.2.3
MySQL version - 5.0.45
magic_quotes_gpc - On

I'm using the edit script below. When the form is displayed with the information to be edited - if there is a single quote in the title it (the title) gets messed up.

When this script is first called it displays a list of all the titles in the database with an Edit ---- Delete link next to it.

At this point the title is displayed correctly. For example:
$title = The World's Fastest Car
What's displayed = The World's Fastest Car

The 's after World is there. Good


When you click on Edit next to a title, a form is displayed with all the information from the database.

Everything shows up OK except for the title. For example:
$title = The World's Fastest Car
What's displayed and re-entered into the database when edit is submitted = The World Fastest Car

The 's after World is now gone. I've tried several things but can't seem to overcome this problem. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("includes/dbconnect.php");
  3. if(!isset($cmd)) 
  4. $result = mysql_query("select * from cool order by id desc"); 
  5. while($r = mysql_fetch_array($result)) 
  6. $title = stripslashes($r['title']);
  7. $source = stripslashes($r['source']);
  8. $content = stripslashes($r['content']);
  9. $id = $r['id']; 
  10.  
  11. echo "
  12. $title [ <a href='edit_cool.php?cmd=edit&id=$id'>Edit</a> ]
  13. [ <a href='edit_cool.php?cmd=delete&id=$id'>Delete</a> ]
  14. <br>
  15. "; 
  16.  
  17. if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
  18. if (!isset($_POST["submit"])) 
  19. $id = $_GET["id"]; 
  20. $sql = "SELECT * FROM cool WHERE id='$id'";
  21.  
  22. $result = mysql_query($sql); 
  23. $myrow = mysql_fetch_array($result);
  24.  
  25. $title = stripslashes($myrow['title']);
  26. $source = stripslashes($myrow['source']);
  27. $content = stripslashes($myrow['content']);
  28. $id = $myrow['id'];
  29.  
  30. echo "
  31. <form action='edit_cool.php' method='post'>
  32. <input type=hidden name='id' value='$id'> 
  33.  
  34. Title:<br>
  35. <input type='text' name='title' value='$title' ><br><br>
  36.  
  37. Source:<br>
  38. <input type='text' name='source' value='$source'><br><br>
  39.  
  40. Content:<br>
  41. <textarea name='content' rows=30 wrap=virtual>$content</textarea><br><br> 
  42.  
  43. <input type='hidden' name='cmd' value='edit'> 
  44. <input type='submit' name='submit' value='submit'> 
  45. </form> 
  46. ";
  47. }
  48.  
  49.  
  50. if ($_POST["$submit"]) 
  51. $title = escape_data($_POST['title']);
  52. $source = escape_data($_POST['source']);
  53. $content = escape_data($_POST['content']);
  54.  
  55. $sql = "UPDATE cool
  56. SET title='$title',
  57. content='$content',
  58. source='$source'
  59. WHERE id='$id'"; 
  60.  
  61. $result = mysql_query($sql); 
  62. echo "Information updated.";
  63.  
  64. }
  65. }
  66. ?>
Jan 26 '09 #1
2 2377
DavidPr
155 100+
OK, I fixed problem - found on line 41 above.

41. <input type='text' name='title' value='$title' ><br><br>

changed to this:

41. <input type='text' name='title' value=\"$title\" ><br><br>

I do this echo " and wrap my variables with single quotes.
Jan 26 '09 #2
Atli
5,058 Expert 4TB
Hi.

When you have large amounts of HTML that you need to print, like in your code, you are better of using Heredoc syntax.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Set up some values to put into the HTML
  3. $theTitle = "The tile of the page";
  4. $theText = "Some text to display";
  5.  
  6. // Print some HTML
  7. echo <<<HTML
  8. <html>
  9.   <head>
  10.     <title>{$theTitle}</title>
  11.   </head>
  12.   <body>
  13.     <h1>{$theText}</h1>
  14.     <p>
  15.       You can use all sorts of quotes in here
  16.       without causing any syntax problems.
  17.       Like: John's name.
  18.       Or: John said: "What?"
  19.     </p>
  20.   </body>
  21. </html>
  22. HTML; // Ends the text. Must be the first thing in the line.
  23. ?>
Jan 26 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
3
by: Mats | last post by:
It's good practice to validate input, not only where it should be coming from, but from anywhere it's possible to change or add input for a "client". If all user input is transfered using "post"...
1
by: Steve | last post by:
Hi; I have a table with a TEXT datatype. Its a comment field. Right now the users who put in singlequotes are killing the web front end. The programmer responsible is fixing this issue but...
8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
3
by: Fred Flintstone | last post by:
I'm writing an app in VB.Net that talks to MS SQL Server 2000. The problem is that when someone enters a single quote into a field, I get SQL errors. I've looked this up and its recommended that...
3
by: David C. Barber | last post by:
Using SQL Server 2000 and moving to a new computer. We did a full backup of the existing database to tape, brought up the new computer with a clean install using the same server name and IP...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
2
by: Reporter | last post by:
I got the following example from http://www.evolt.org/article/User_Friendly_Forms_in_PHP/20/60144/index.html : echo '<tr><td>First name:</td><td><input type="text" name="first_name"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.