473,795 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting text with quotes in to MYSQL

19 New Member
Hello all,

I have a problem with inserting text with quotes, or html code in to a MYSql Database. I've been trying to create my own content management system, and unfortunately I've come accross a problem, the script below works perfectly if I'm not inserting any quotes (" ") or html tags. I'm wondering if I'm doing something wrong here? The field type where all of the content will go was set up as a blob type.

I have the following PHP code:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $conn = connect();
  3. $action = $_GET['a'];
  4. $id = $_GET['id'];
  5. switch($action) {
  6. case 'delete':
  7.     $sql = "DELETE FROM article WHERE id='$id'";
  8.     if(mysql_query($sql)) {
  9.         echo "<script type='text/javascript'> alert('Article Deleted'); </script>";
  10.         header("Location: article.php");
  11.     }    
  12. break;
  13.  
  14. case 'add':
  15. if(isset($_POST['submit'])) {
  16. $title = $_POST['title'];
  17. $text = $_POST['content'];
  18.     $sql = "INSERT INTO article (articleTitle,articleContent) values ('$title','$text')";
  19.     if(mysql_query($sql)) {
  20.         echo "<script type='text/javascript'> alert('Article Added'); </script>";
  21.         header("Location: article.php");
  22.     }
  23. }
  24. break;
  25.  
  26. case 'edit':
  27. if(isset($_POST['submit'])) {
  28. $title = $_POST['title'];
  29. $text = $_POST['content'];
  30.     $sql = "UPDATE article SET articleTitle='$title',articleContent='$text' WHERE id='$id'";
  31.     if(mysql_query($sql)) {
  32.         echo "<script type='text/javascript'> alert('Article Updated'); </script>";
  33.         header("Location: article.php");
  34.     }
  35.     }
  36. break;    
  37. }
  38. ?>
Thanks for any help!
Mar 26 '09 #1
5 13109
Markus
6,050 Recognized Expert Expert
Have a look at SQL Injection and mysql_real_esca pe_string().
Mar 26 '09 #2
atlanteavila
19 New Member
Hello Markus...Thank you for a speedy reply. I'm a bit confused, where would I put the mysql_real_esca pe_string()?


Expand|Select|Wrap|Line Numbers
  1. case 'add': 
  2. if(isset($_POST['submit'])) { 
  3. $title = $_POST['title']; 
  4. $text = $_POST['content']; 
  5.     $sql = "INSERT INTO article (articleTitle,articleContent) values ('$title','$text')";
  6. mysql_real_escape_string($title, $text); 
  7.     if(mysql_query($sql)) { 
  8.         echo "<script type='text/javascript'> alert('Article Added'); </script>"; 
  9.         header("Location: article.php"); 
  10.     } 
  11. break; 
also, do I need to add the magic quotes attribute? If so, would you mind giving me an example of the code to add magic quotes? this is something I havent been able to figure out.

Thanks!
Mar 26 '09 #3
Markus
6,050 Recognized Expert Expert
No, mysql_real_esca pe_string() will take care of it.

You need to use mysql_real_esca pe_string() on any data you are inserting into the database before it is inserted.

Expand|Select|Wrap|Line Numbers
  1. $example = mysql_real_escape_string( $_POST['example'] );
  2.  
  3. mysql_query( "INSERT INTO `tbl` VALUES( '{$example}' )";
  4.  
Mar 26 '09 #4
atlanteavila
19 New Member
Thank you mark that worked!
Mar 28 '09 #5
Markus
6,050 Recognized Expert Expert
@atlanteavila
You're very welcome :D

- Markus.
Mar 28 '09 #6

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

Similar topics

1
3032
by: kalbee | last post by:
When we insert text into field in a table, SQL SERVER apparently replaces apostrophes with question marks -- is there a way to not have this occur? We don't have this happen with the mySQL databases that we also support. Much help appreciated.
6
2625
by: Raqueeb Hassan | last post by:
Hello There! Have you guys tried inserting variables in mySQL tables? Do I have to use 'quote' as we had been doing to insert strings? mysql> INSERT INTO occurrence (word_id,page_id) VALUES ($word_id,$page_id); ERROR 1054: Unknown column '$word_id' in 'field list'
2
8914
by: Hendrik | last post by:
Hi I have searched extensively for help on inserting text at the position of the mouse pointer in a text box as the final step of drag and drop process What I have come with is listed below - the problem is that the SendMessage function doesn't seem to perform the expected operation of moving the caret to position in the textbox where the mouse is pointing to. (can be text or richtext box either is good - sample code is based of attemp...
2
2424
by: csgraham74 | last post by:
Hello, im using MS Access as a front end to link to MYSQL. although this is possibly an Access issue i thought someone would be able to help me. the error message "ODBC--insert on a linked table 'tablname' failed" is displayed when i attempt to insert records to the MYSQL table. Does anyone know of a way round this ?? im using the most recent MYSQL driver and i have only text fields in my insert statement.
1
11772
by: tomi.trescak | last post by:
Hi I have a problem with storing rich text in MySQL. I store rich text in MySQL (in column with type "text") which i get from Rich Textbox control. When i do reverse processing by trying to fill rich text back to rich techbox (i set correct property "Rtf") i get whole text like: { tf1ansiansicpg1250deff0deflang1029{fonttbl{f0fnilfcharset0
1
1429
by: Aido82 | last post by:
Im having a problem inserting the following data into a Mysql table, just wondering if any one has any ideas on how to get it to work. When i click the register button nothing at all happens . any one got any ideas. <?php $connection = mysql_connect("localhost", "root", "") or die ("Could not connect to server"); $db = mysql_select_db('customer', $connection) or die('could not connect to DB' .mysql_error());
8
2477
by: gomzi | last post by:
hi, Is there a way for inserting double quotes in a texbox? i.e. i would like achieve something like this -> Gomzi"Gomzi (should be displayed in a textbox). Tried both using backslashes and html encode without much success.
0
1526
by: ajithmanmadhan | last post by:
hi pls help me out in inserting text into a richtextbox. the situation is like this:: i want to insert a string say "hello" at a particular location in the rich textbox. The particular location is given in the form of X and Y coordinates. eg:: PutString("hello",10,20) i write a function that passes the string and x,y coordinates. is that possible?? x,y coordinates will represent the location of the string in richtextbox.
2
4815
by: Nilanjan Sanyal | last post by:
how to retrieve hindi text from mysql database using php
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.