This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have a problem with inserting proper data into my DB with php...
I am trying insert some data with this code:
$query1 = "insert into `exercises`(`exercise_name`) values ('".$txtEName."');";
$query2 = "insert into `exercises_type`(`type_name`) values ('".$txtEType."');";
mysqli_query($connnect, $query1);
mysqli_query($connnect, $query2);
all data is russian text... all data is saving into the DB, but, in DB it looks like
so what I am doing wrong?
You can check for correct charset/collation. As this may help you to insert correct data in you table. also you can use character encoding for values your are getting by $_POST.
This link may help you
Related
This question already has answers here:
What's the best way to store Checkbox Values in MySQL Database?
(3 answers)
Closed 2 years ago.
I created a form with multiple checkboxes to add to database, what do I need to change in the VALUE $_POST[penerima] to enter multiple checkboxes data into the database?
if(isset($_POST['simpan'])){
$simpan = mysqli_query($koneksi, "INSERT INTO umum (tanggal_terima, pengirim, no_surat, perihal, disposisi, penerima )
VALUES ('$_POST[tgl_terima]',
'$_POST[pengirim]',
'$_POST[no_surat]',
'$_POST[perihal]',
'$_POST[disposisi]',
'$_POST[penerima]')
");
I'm not going to address the SQL injection issues in the code, but please read:
How can I prevent SQL injection in PHP?
You can store array data in a database using json_encode($array) - this will convert your array of checkboxes to a string which can be stored. I suggest using the TEXT column type, otherwise, the sting may be cut off if your values are long.
https://www.php.net/manual/en/function.json-encode.php
You can then json_decode the data back to an array once it's selected.
https://www.php.net/manual/en/function.json-decode.php
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
I have a table with contact codes. When contact code is not specified there are ^^ in the empty cell. If specified should be: ^Code123^,^Code321^,^Code987^
I want to update this cell, but I dont know how to rewrite empty cell with ^^?
With my query I get this result ^^,^Code123^,^Code321^,^Code987^. How to delete ^^ when updating cell?
$sql6 = "UPDATE contacts
SET clicks_c=IF(clicks_c='',
'^".$url_name."^',
CONCAT_WS(',',clicks_c,'^".$url_name."^'))
WHERE id_c='".$row_id."'";
If an empty cell contains ^^, you need to test for that in your IF(). Change:
IF(clicks_c='',
to:
IF(clicks_c IN ('', '^^),
Then it will replace ^^ with a nonempty code, instead of concatenating to it.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 4 years ago.
राम (hindi language)
I am working on a table where many rows contains value like this राम it shows in browser like राम and many rows already contains राम but
I want to replace all encoded value with decode values for this i used find replace query also put data in form and updated but every time db stores encoded value
Structure of table
hindi varchar(256) utf8_unicode_ci
what i did
<?php
echo $queryfetch="select hindi from users where id='2'";
$resultfetch=mysqli_query($c,$queryfetch);
while($row = mysqli_fetch_array($resultfetch))
{
echo $queryupdate="update users set hindi='".html_entity_decode($row['hindi'])."' where id='2'";
$resultupdate=mysqli_query($c,$queryupdate);
}
?>
select hindi from users where id='2'
update users set hindi='राम' where id='2'
**
sql before =राम
**sql after = राम**
What you have are html-encoded characters. To decode them, you need:
html_entity_decode('राम')
An example.
This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
Maybe a silly question, but I'm really struggling.
I've created a MySQL Database and Table and am trying to push data into it. The data in question is a unique ID and a load of Javascript.
Code is simple:
$sql = "INSERT INTO TableName (id, code)
VALUES ('$uniqueid', '$codeoutput')";
However, whilst I can get the ID in there, I can't get the DB to accept the Javascript (which is currently stored in the variable $codeoutput.
I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
I'm not that familiar with using MySQL, and I have played around with different data types in PhpMyAdmin, but sadly still no luck.
What am I doing wrong? Do I need to have a very specific setup on the column where I'm storing the code? I'm currently trying to store it as medium text.
Is it something to do with needing to escape special characters? Is there an easy solution to this?
simplest way to do it is
$sql = "INSERT INTO TableName ($uniqueid) VALUES ('id')";
$sql2 = "INSERT INTO TableName ($codeoutput) VALUES ('code')";
This question already has answers here:
How to insert utf-8 mb4 character(emoji in ios5) in mysql?
(2 answers)
Closed 6 years ago.
So I use the following line of code to insert a chat message into my MySQL database:
$this->db->query("INSERT INTO group_messages (group_message_text,group_message_group_id,group_message_user_id) VALUES ('$message','$group_id','$user_id');");
This works great until the users tries to use characters like ' or emoji's. How do I handle that properly?
To store emoji's in your database you have to store it in utf8mb4. See this answer.
You should also escape your text bevor storing it into the database. See this answer as well.
Might be the Syntax error:
Please try using
$this->db->query("INSERT INTO group_messages (group_message_text,group_message_group_id,group_message_user_id) VALUES ('".$message."','".$group_id."','".$user_id."');");
Hope this will help :)