Jump to content

How To Handle Apostrophes


refiking

Recommended Posts

$text = "Steve's text";

$string = addslashes($text);

mysql_query("INSERT INTO table (`text`) VALUES ('".string."')");

 

Then to retrieve

 

$query = mysql_query("SELECT text FROM table WHERE .....");

$text = mysql_fetch_array($query);

 

$text = stripslashes($text[0]);

 

echo $text;

 

Something like that :)

Link to comment
Share on other sites

Infact instead of using addslashes before it goes into the database, I'd increase the security and use

 

function clean($data) {

if (!get_magic_quotes_gpc()) {

$data = addslashes($data);

}

 

$data = strip_tags($data);

$data = htmlspecialchars($data, ENT_QUOTES);

$data = trim($data);

 

return $data;

 

}

 

It's a bit more safe

Link to comment
Share on other sites

OK.  So, I add this function and call my variables.  Then, how would I use this function with those variables.  For example:
[code]
while($row = mysql_fetch_assoc($sql)){
$var1 = $row['field1'];
$var2 = $row['field2'];
$var3 = $row['field3'];
}
//Would I then add this? //
$var1 = clean($var1);
$var2 = clean($var2);
$var3 = clean($var3);

[/code]

 

Link to comment
Share on other sites

You should use stripslashes() before htmlentities():

<?php
while($row = mysql_fetch_assoc($sql)){
     $var1 = htmlentities(stripslashes($row['field1']),ENT_QUOTES);
     $var2 = htmlentities(stripslashes($row['field2']),ENT_QUOTES);
//
// do something with var1 & var2
//
}?>

 

Ken

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.