removing extra characters before inserting data in MYSQL? - php

I'm trying to create a simple blog type system for my site using PHP and MYSQL.
everything works fine. however, the issue that I am facing right now is that when I insert data into mysql database, it will have some extra characters.
the extra character that I am having the most difficulty with is this: \
Example:
when I insert an image link/tag into database: I get the following in my database:
<p><img alt=\"\" src=\"http://somedomain.com/images/9Image1.jpg\" /></p>
so I am trying to remove those extra characters before inserting the data into mysql using PHP.
for that, I tried using the following code:
$body = preg_replace("/\\\\\/", "", $body);
however, this code doesn't do anything. and in fact stops my entire code working and it stops my code from inserting any data into mysql.
Could someone please advise on this issue?
Thanks in advance.

Try to use stripslashes(string)
$body = stripslashes($body);
Let me know if it works.

Related

How to get editor data without any special character in php

I am working with php and i am trying to get data from "Editor" ( like Tinymyc),But
right now i am getting special characters like "<strong> ,<p>" etc..i just want to get simple data
without any special characters,how can i do this ?I tried with following code
$data=$_POST['editor'];
Give this a try - if your code is correct this should work.
$data = strip_tags($_POST['editor']);

Laravel Escape quotes

I am using laravel 4.2 and I am having a very weird problem while saving the data using the model.
I have a text area where user inputs some text and then hit the save button. it is saving fine when the text does not have any quotes in it. But gives issues when the text have some quotes.
For example: If I try to save "Hi. I am Jacky's friend." This text saves fine but when I check in the database there I see this "Hi. I am Jacky\\\'s friend." I get extra back slashes saved in the database.
This is my code :
Suppose $textnotes is the variables in which I am getting the entire text.
$savenotes = new Details(); // Details is the model and the table name is 'Details'
$savenotes->Notes = $textnotes; // 'Notes' is the column
$savenotes->save();
if(!empty($savenotes->id))
// My other stuffs here
I am unable to fix this. Any help will be appreciated.
This could be a PHP setting called Magic Quotes interfering with strings when they contain certain characters, rather than a Laravel specific problem. You can turn this off in your php.ini file, by following instructions in the documentation.

Data in MySQL database doesn't show correctly in website

I am trying to translate a English website to Persian. problems i was facing was :
website were loading in Latin Unicode, so I had to change the charset to utf-8 so contents show correctly in Persian
data in MySQL database are not correctly shown in website probably cause of the Unicode problem
What I have done:
<?php ini_set('default_charset','utf-8'); header('Content-type: text/html; charset=utf-8'); ?>
by this , problem #1 fixed
but for problem number 2 i still facing the issue, although i have altered the tables to use utf 8 , but problem still persists. I gladly like to see how anyone can help me with this.
function bbcode ($str) {
//$str = htmlentities($str);
$token = array(
"'\[b\](.*?)\[/b\]'is",
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
"':big_smile:'is",
"':cool:'is",
"':hmm:'is",
"':lol:'is",
"':mad:'is",
"':neutral:'is",
"':roll:'is",
"':sad:'is",
"':smile:'is",
"':tongue:'is",
"':wink:'is",
"':yikes:'is",
"':bull:'is",
'/\[item\=(.*?)\](.*?)\[\/item\]/is',
'/\[spell\=(.*?)\](.*?)\[\/spell\]/is',
"':warrior:'is",
"':paladin:'is",
"':hunter:'is",
"':rogue:'is",
"':priest:'is",
"':dk:'is",
"':shaman:'is",
"':mage:'is",
"':warlock:'is",
"':druid:'is",
"'\[ul\](.*?)\[/ul\]'is",
"'\[ol\](.*?)\[/ol\]'is",
"'\[li\](.*?)\[/li\]'is",
);
thanks alot in advance
Sorry, my reply wasn't clear enough. I was almost sleep. The databases are empty, so I don't have to convert anything, but when I am inserting data into them, the data doesn't appear correctly. BTW, I'm not good with php or mysql; I am reading these articles and suggestions for hours and I'm just getting more confused. Can you just tell me where should I enter the code and what code,
$link = mysql_connect("localhost","UserName","Password") or die(mysql_error());
mysql_set_charset("utf8",$link);
mysql_select_db("DataBase Name") or die(mysql_error());
I guess the thing I found out from these articles is to add the mysql_set_charset("utf8",$link) part to the above code while the server tries to connect to db, but I have tried that and its not working. My website uses includes so thats like this:
include("../../config/config.php");
$connect = mysql_connect("$db_host", "$db_user", "$db_pass")or die(mysql_error());
mysql_set_charset("utf8",$link);
Assuming you've correctly converted the data in your tables to UTF-8 (just changing the character set is not enough), it sounds like you might be having problems with the connection not being set up as UTF-8. Have a look at SET NAMES, and more specifically this question.
If you're not sure you've converted your data to UTF-8, I'd have a look at this question as well as this Wordpress article and make sure you've followed the steps.

Mysql insert from XML with "hyphen" in field name

I am trying to insert into mysql from an xml using PHP, but the field name contains a "hyphen" and for some reason it is not inserting this field, the rest are fine. I have tried using the hex codes x002D, x2010,x2012, and none have worked. As you can see in my xml, the hyphen is not being changed like the space, but if I just remove the x002D inserts everything after the "E" in other words just inserts -mail_x0020_Address.
This is my xml:
<encuestas>
<ID>9949</ID>
<E-mail_x0020_Address>email#email.com</E-mail_x0020_Address>
<ZIP_x002F_Postal_x0020_Code>90001</ZIP_x002F_Postal_x0020_Code>
</encuestas>
This is my insert statement ( I removed fields, I have 20 on that xml):
"INSERT INTO New_Encuestas_Datos(ID,`email`,`Zip/Postal Code`) VALUES('$product->ID','$product->E-mail_x0020_Address','$product->ZIP_x002F_Postal_x0020_Code')";
The variables are coming from an array reading the xml and like I said, everything is imported except email. I have tried the following combinations:
E-mail_x0020_Address
E_x002D_mail_x0020_Address
E_x2010_mail_x0020_Address
E_x2012_mail_x0020_Address
Can anyone point me what I am doing wrong? Thanks !
Hmm, I doubt you can use it in a string, but in plain PHP it is $product->{'E-mail_x0020_Address'};. But that doesn't matter anyway, as you should escape you values prior to sending them to a database you can nicely name an escaped variable as you like.
Try using:
"INSERT INTO New_Encuestas_Datos(ID,`email`,`Zip/Postal Code`) VALUES('{$product->ID}', '{$product->E-mail_x0020_Address}', '{$product->ZIP_x002F_Postal_x0020_Code}')";
Edited:
Just found here Validate class/method names with regex that '-' is not valid in method names.

how to insert ampersand in mysql using php?

How to insert ampersand in mysql using php?
Thanks in advance
When I execute the following code the “one” is saves in database but the “ second” and ampersand sign won’t. plz help me…
$companyName ="one & second";
INSERT INTO registeration(companyName) VALUES('". $companyName ."')";
I know this is old but I just ran into this issue myself and the issue turned out to be an escaping issue with ajax post rather than an issue with mysql and php. I added encodeURIComponent(variabletopost) to the variable before posting it with ajax and it worked great.
Exactly the same way as any other data, no special action required.
insert into table (field) values ('&')
I know it's an old post but,
try adding addslashes() to your php var:
$companyName = addslashes("one & second");
Then, when php parses the '&' (ampersand) , it reads '\&' (backslash-ampersand) instead of trying to create a reference in the middle of the query.
I also faced this type of issue and solved by using javascript function encodeURIComponent(uri);
In place of uri you have to put your variable. then it will allow all special character.

Categories