$__POST[string] to receive a unicode string fails - php

Unable to perform the query statement Incorrect string value: \x96\xBC\x91O\x82\xCD... for column 'member' at row 1.
Here is the code.
$member=stripslashes($_POST["membername"]);
$link=mysql_connect(localhost,$username, $password) or die("unable to connect to database ".mysql_error());
mysql_set_charset('utf8', $link);
mysql_selectdb($database) or die("unable to select db ".mysql_error());
$query="SELECT*FROM members";
$result=mysql_query($query);
$rowNum=mysql_num_rows($result)+1;
$query="INSERT INTO members VALUES ('$rowNum','$member','$memberemail','$memberpass','$memberRegDate','$memberRegDate','$memberIP')";
mysql_query($query) or die("Unable to perform the query statement. ".mysql_error());
EDIT; I already change my DB as this
ALTER DATABASE users CHARACTER SET UTF8 COLLATE UTF8_UNICODE_CI;
and
ALTER TABLE members CHANGE name name varchar(100) character set utf8;

That's not UTF-8. You should be looking at the HTTP headers in the request to see what charset is being used, and convert appropriately.
>>> print '\x96\xBC\x91O\x82\xCD...'.decode('cp949')
뼹멟궼...

Related

MySql,Php not set to UTF8

I'm inserting utf-8 data to the database with this code
$conn = new mysqli($hostname, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$sql = "INSERT INTO Movies (Name, Year)
VALUES ('".$_POST["name"]."', '".$_POST["year"]."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
But the data is added like this:
What should I do?
This is something I think you must fix in the back end, the PHP code is perfect, but you need to configure your database to utf-8 as well. You appear to be using PHPmyAdmin, when you create or import the table, you should see a drop down box to select the file's character set. Change this and you should be good to go!
That should start (end?) with Arabic 'noon' (ن), correct?
ن is the "html entity" for 'noon'.
Was the user filling in an html <form>? does it include a charset, such as <form accept-charset="UTF-8">? If not, that might be the solution.
Is the column/table declared CHARACTER SET utf8 (or utf8mb4, but not utf32)? It needs to be. See SHOW CREATE TABLE.
You can use alter command in mysql to change all the fields to utf8 format,
Please check the sql query
ALTER TABLE `Movies` CHANGE `Name` `Name` VARCHAR(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;

How to get output from mysql in right encoding

how can i get MYSQL output in right encoding? I have this escaped string in MYSQL DB: Wannahëal, i need Wannahëal.
Thank you!
// Connect
$link = mysql_connect('xxx', 'xxx', 'xxx') or die("Can't connect to MYSQL DB");
mysql_set_charset("UTF8", $link);
//Choose a DB
mysql_select_db('xxx') or die("Can't select MYSQL DB");
$query = "SELECT * FROM mr_characters";
$result = mysql_query($query) or die('Query failed!');
header('Content-type: text/html; charset=utf-8');
while($row = mysql_fetch_object($result))
{
echo $row->title . "<br>";
}
You can use mysql_set_charset to set your client character set.
You need to set the charset in your charset, I think in your case utf8.
Either you can make this in the creating of your db:
CREATE DATABASE myDB DEFAULT CHARACTER SET utf8;
[...]
Or in the tables:
[...]
CREATE TABLE myTable DEFAULT CHARACTER SET utf8;
(
[...]
EDIT: If you don't set the charset before you make the data input into your MySQL server, your MySQL server isn't able to read the data correctly and saves them incorrectly. So not your code is incorrect nor your server had an any failure, quite honestly your server saved your data incorrect.

MySQL insert does not work

I want to insert data in mysql database.when i am trying to insert my query using PHP MyAdmin then work but if i am tring to insert from my php site from submission the not work some text not insert.
my text
http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:
my form submission insert only this text
http://www.lexisnexis.com/hottopics/gacode/
my query
$test='http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:';
$conn = mysql_connect('localhost', 'test', 'test') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query('SET NAMES utf8');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection ='utf8_unicode_ci'");
$queryc = "INSERT INTO `table` (data17)values ('".addslashes($test)."')";
mysql_query($queryc) or die(mysql_error());
for the database connection, it should be:
$conn = mysqli_connect('localhost', 'test', 'test') or die(mysqli_error($conn));
for the query, it should be:
mysqli_query($conn, $queryc) or die(mysqli_error($conn);
remember, the mysqli_query's arguments are database connection, query
also, it would be better if the query ($queryc) was:
$queryc = "INSERT INTO `table` (data17)values mysqli_real_escape_string($test))";

Using PHP to INSERT a defined variable into MySQL DB/table, no data is (apparantly) being written to DB?

Thanks for taking a look:
Here is the php I'm using to insert the data into the table
<?php
session_start();
//sets a variable from a session value
if (isset($_SESSION['sv_01'])) {$sv_01=$_SESSION['sv_01'];} else {$sv_01="";}
//to test that the variable has been set and is not empty
echo $sv_01;
//define database log in stuff
$username="username123";
$password="password123";
$database="database01";
$table="my_table";
$dbaddress="123.123.123.123";
//connect to dbserver
$con=mysql_connect($dbaddress,$username,$password);
if (!$con)
{
die('Could not connect:' .mysql_error());
}
//select the db
mysql_select_db($database) or die( "Unable to select database");
//insert data from variables
mysql_query("INSERT INTO $table
(
$sv_01
)
VALUES
(
'$sv_01'
)");
mysql_close($con);
?>
I run this, and then go to check out the contents of the DB. Using MySQL workbench I open the connection and the database and table in question, select all rows and there is no data contained in the table.
MySQL info:
Collation: latin1 - default
collation Engine: MyISAM
datatype: sv_01 VARCHAR (255)
default: NULL
Any ideas what I am doing incorrectly?
I believe that the name of the field is sv_01 not $sv_01
I would try:
$query = "INSERT INTO $table (sv_01) VALUES ('$sv_01')";
Update (dedicated to tadman):
A small piece of advice: DO NOT use mysql_query
Use localhost insted af your IP (if possible), and make your connection easy to read:
$con=mysql_connect($dbaddress,$username,$password) OR DIE mysql_error();
AND you also have to give you mysql_query a variable:
$mysql = mysql_query("INSERT INTO $table ($sv_01) VALUES ('".$sv_01."');");
:)

Insert into two tables from a single form

Insert into two tables from a single form. The first insert go in fine the second generates this error Duplicate entry '0' for key 1 any idea what is happening?
$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db=mysql_select_db ("database", $connection) or die (mysql_error());
$query = "INSERT INTO worklog (id, newtime, datetime, clientname, clientcode, startmo, startday, startyr, endmo, endday, endyr, duemo, dueday, dueyr, market, job, allTypes, spec, status, designer, dsgnemail, adrep, ademail, frame1, frame2, frame3, rush) VALUES ('$id', $newtime, now(), '$clientname', '$clientcode', '$startmo', '$startday', '$startyr', '$endmo', '$endday', '$endyr', '$duemo', '$dueday', '$dueyr', '$market', '$job', '$allTypes', '$spec', '$status', '$designer', '$dsgnemail', '$adrep', '$ademail', '$frame1', '$frame2', '$frame3', '$rush')";
$sql_result = mysql_query($query, $connection) or die (mysql_error());
$worklog_id=mysql_insert_id($connection);
$connection2=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db2=mysql_select_db ("database", $connection2) or die (mysql_error());
$query2 = "INSERT INTO worklognotes (worklog_id, spec) VALUES ('$worklog_id', '$spec')";
$sql_result = mysql_query($query2, $connection2) or die (mysql_error());
I thin the culprit is the line:
$worklog_id=mysql_insert_id($connection);
according to the PHP documentation:
"The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established"
So if the id in worklog is not AUTO_INCREMENT it will always return 0 ... your second attempt at running the code will cause:
Duplicate entry '0' for key 1
Two ways to fix this:
id for worklog should be AUTO_INCREMENT ... this way mysql_insert_id will return ther ID generated by the database and you can use it as a working id for the next query
just use $id instead of $worklog_id
normally with and table ID column you set it to auto-increment and never explicitly insert it. The database management system will take care of inserting that column. The error means that you are inserting a row that has that ID already, meaning the column has a UNIQUE constraint.

Categories