I am posting Comments in hindi to another page, where i insert to db, but the comments is getting saved as junk in mysql.
--PHP
<form method='post' action='post.php'>
<textarea name='comments'></textarea>
</form>
--post.php
$comment =$_POST["comments"];
$query = "INSERT INTO comments SET comment='".mysql_escape_string($comment)."' ";
mysql_query($query) or die(mysql_error());
In DB the text gets inserted as junk characters
the database is of correct type as i have inserted the html notation on हिंदी { hindi characters to be saved} , and display works with correct META type,
but DB entry is like à╧√à╧¿à╧?à╧▌à╛?
i tried an insert query in the same file and it worked, but posting to some other file creates the problem, is it someting to do with posted data ?
i cannot use same file for insertion as other forms are there in the same page, which are handled.
You should try setting your "file encoding" to UTF-8 and so does your database encoding.
Most people forget that a file also has an encoding type. Especially on Windows this will bring you trouble.
Related
It's new for me to use richtext but a user has to be able to insert pictures or to change his text. So I'm using ckeditor but i've some trouble with it.
When a user write into the rich text he can write things using apostrophe and comas.
I'll use an example. Let's imagine i want to write :
It's quite difficult to open a picture, a file blablabla
The problem is that in the inserting query looks like that
Insert into tab (txt1,txt2) values ('value1','value2')
If the user uses comas or apostrophe the richtext cannot be correctly inserted. Moreover, I use longtext in my MySQL database to store the text.
My questions are :
How to store the richtext written by the user
What type should be the column in my MySQL database to correctly store the richtext to be able to give it back
The problem i have is
Erreur SQL !INSERT INTO detail_article(ID,Problem,Num,URL,Solving,Description) VALUES(16,'Chrome : clearing datas',4,'','
\r\nCHROME CACHE CLEARING :
\r\n\r\n
blablabala
\r\n\r\n
In the 'windows explorer', do the fallowing statment....blablabla
\r\n','')
It's a sample of my text but i think the problem is caused by the quotes and the coma
Your problem must come from your PHP script which seems not to escape the pieces of data it send.
If it's not working with the richtext containing commas and apostrophes, then it's probably that the query send to your database isn't escape.
For example, if you have such content :
$value2 = "Catch',em all";
$value1 = "Hello boys";
And if you try to store it in database without escaping it, then the resulting SQL query will looks like :
INSERT INTO tab (value1, value2) VALUES ('Hello boys', 'Catch', em all);
which will be an incorrect SQL statement because of incorrect syntax.
If you use some escape method like PDO::quote(), your input will be inserted in a safe way that won't produce any error (syntax).
Make sure your script does escape the params you send with either PDO::quote() (https://php.net/manual/fr/pdo.quote.php) or either mysqli_real_escape_string (http://php.net/manual/fr/mysqli.real-escape-string.php)
I have a mySQL db table. One of the table columns contains URLs which point to different xml files on a remote server.
My goal is to read each URLs info and write the xml content into another column on the same record (line) respectively.
In my PHP code, I am able to get the URL correctly from mySQL database and I am able to get the XML content on remote server into a variable correctly.
But the issue is while I write the content to my table line by line. Some XML columns got update correctly and some XML columns are empty.
I am pretty sure each time the variable got content correctly because I am able to print out each individual content on screen.
Why are some content updating the column and some don't. All the XML strings have the same format. If I copied that content and updated the mysql table manually, it successfully wrote into the table.
At beginning I thought it was time issue so I add enough sleep time for my PHP code. it does't help. then I suspected my db datatype, so I changed the XML
column data type from VCHAR to TEXT and even LONGTEXT. it does't help either. Does any one have a clue?
part of my php code below...
$result = mysqli_query($con,"SELECT url_txt FROM mytable ");
//work with result line by line:
while($row = mysqli_fetch_array($result)) {
echo $url_content = file_get_contents($row['url_txt']);
//debug line below *******************************/
echo $URL=$row[url_txt];
//debug line above********************************/
mysqli_query($con,"UPDATE mytable SET xml_info='$url_content' where url_txt = '$URL' ");
}
Maybe try converting your XML to a native array and working with it that way:
$array = json_decode(json_encode(simplexml_load_string($url_content)),TRUE);
I have a mysql database with an 'address' field (VARCHAR). I also have a html table that is used to display the addresses of different staff members.
My problem is that data is stored in the address field of the database with linebreaks between each line of the address, but when the data is displayed in the table the line breaks are not there, so the entire address is displayed on one line. How can I fix this?
I'm sure this is a question that is asked all the time, but I can't find the answer anywhere. I'm new to PHP so please forgive my naivety.
UPDATE:
This is basically my code:
if(isset($_POST['submit'])) {
$address = $_POST['address'];
$queryupdate = "UPDATE Staff SET
address= :address WHERE id= :id";
$q = $db->prepare($queryupdate);
$q->execute(array(
":id" => $id,
":address" => $address));
The data in the $address variable is taken from a simple textarea.
Actual line breaks are never shown in HTML unless the word-wrap is set to pre, or an actual pre tag is used. However, to overcome this you can use the nl2br() functionality in PHP. What you'll need to do is use the nl2br() before outputting your data to the browser, and it will give you a HTML formatted string back where the line breaks are prepended by <br> tags.
See the documentation about nl2br() here: http://php.net/manual/en/function.nl2br.php
Using help from #Boy :
I added 'nl2br' before the output of my string.
Relevent code:
$status = $db->run(
"INSERT INTO user_wall (accountID, fromID, text, datetime) VALUES (:toID, :fromID, :text, '" . time() . "')",
array(":toID" => $toID, ":fromID" => %accountID, ":text" => $text)
);
I am taking input text from javascript, throwing it in an AJAX call to handle it, which calls a function which includes these lines of code.
The text string in question is: "Türkçe Türkçe Türkçe!"
Upon investigating the database, the following value is saved "Türkçe Türkçe Türkçe!", which is double utf8_encode'd.
When viewing the text by SELECTing it from the database, I get "Türkçe Türkçe Türkçe!", which is how they should be saved in the database in the first place.
As far as I know, I am not encoding the data as it is being prepared by PDO...
Encoding is a b*tch. You need to make sure it is as you expect it to be in several places:
The HTML page with the Javascript. Set it to utf-8 with a meta tag like:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
The connection to your database. Execute the query set names 'utf8' after connecting to the database (and before any other queries).
Your database field. In MySQL it's called a collation set it to utf8_general_ci (ci stands for case-insensitive).
If you have these 3 your data should always be, and stay, utf-8 (unless you're doing encoding yourself).
For good measure, make sure your source code files are utf-8 as well. Windows typically defaults to iso.
I have made a script to send messages. But the problem is it fails to insert into table when i type a bit longer message though i have set "mail" as VARCHAR and length 30005 .
I am using this query ..
mysql_query("INSERT INTO `mailbox` (`id` ,`receiver` ,`mail` ,`sender` ,`time` ,`date` ,`reply-from` ,`read-status` ,`sd` ,`rd`) VALUES ('', '$receiver_username', '$mail', '$sender_username', '$gmt_time', '$gmt_date', '$from_mail', '1', '', '')") or die("Couldnt Insert Data");
when type small message it is ok. otherwise it shows Couldnt Insert Data .
So please help me.
Recently i have checked its hapening bcz this two syntax ' and " . Now how two insert them i dont want to encode the texts
Length 30005 for a varchar field? that's impossible, varchar's max length is 255 characters. Try making the mail field a TEXT datatype.
You need to look on couple of things here.
1: You should save data data in database in text column rather varchar 30005
2: Look at all followings in PHP.ini file
upload_max_filesize:50M
max_execution_time:NONE
memory_limit:60M
post_max_size:55M
3: More help could be provided after you show actuall error by mysql_error()
Perhaps the long text you are trying to insert has singles quotes ' or other special characters in it.
To fix that, and also for security reasons, you must use mysql_escape_string on your text before trying to insert it into the table.