Jump to content

syntax question


182x

Recommended Posts

hey guys I am using the following syntax to insert records into my database however if one of the records contains an apostrophe the sql statement will not work I was just wondering why and how to fix it? Thanks.

 

 $test="INSERT INTO test VALUES ('','{$testing['cId']}','{$testing['user']}')"; 

Link to comment
Share on other sites

Hi there,

 

I sure ain't no genius, but here goes... MySQL reads apostrophes as part of the query input, so it will try to process them in the query submitted to the database. This often ends with a replacement character such as a question mark, depending on the server settings.

 

Fortunately, you do have many options on what you want to do with them when submitting data to the database. There are several built-in functions within PHP which will either convert apostrophs and other characters into db-friendly characters, add more apostrophe's before or after them to make "doubles" or simply strip them out altogether.

 

Take a look at the following:

 

http://uk.php.net/get_html_translation_table

http://uk.php.net/add_slashes

 

You will have to process the data for submission before it goes into your query, like so:

 

<?php

$str = $testing['cId'];
echo stripslashes($str);

$test="INSERT INTO test VALUES ('$str','$somethingelse')"; 
?>

 

There are many more functions that can be suited for this task, and I'm sure someone else can suggest more.

 

Hope that helps!

Regards,

Iceman

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.