I have a form that saves a name for product category, and since it's in cyrillic I'm having some issues with it. The problem is that it transfers all my cyrillic to some jiberish.
<?php
$con = mysqli_connect('localhost', 'root', '', 'mydb');
// Check connection
if (!$con) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($con,"utf8");
$data = json_decode(file_get_contents("php://input"));
$id = '';
if(!empty($data->CategoryId)) {
$id = mysqli_real_escape_string($con, $data->CategoryId);
}
$name = mysqli_real_escape_string($con, $data->CategoryName);
$link = str_replace(" ", "-", $name);
$link_lower = strtolower($link);
if ($id != '') {
$sql = "UPDATE `categories` SET CategoryName='$name', CategoryLink='$link_lower' WHERE `CategoryId`='$id'";
if ($con->query($sql) === TRUE) {
print 'success';
} else {
print 'error';
}
} else {
$sql = "INSERT INTO `categories` (`CategoryName`, `CategoryLink`) VALUES ('$name', '$link_lower')";
if ($con->query($sql) === TRUE) {
print 'success';
} else {
print 'error';
}
}
?>
I have set the collation to my entire DB and it's tables to utf8_unicode_ci.
if I set a var_dump($name)
before the INSERT INTO
query it returns what I've typed in to the field. But when I set a var_dump($sql)
, both $name
and $link_lower
are in some jiberish
'INSERT INTO `categories` (`CategoryName`, `CategoryLink`) VALUES ('Ðобилни ÑÑÑÑойÑÑва', 'ðð¾ð±ð¸ð»ð½ð¸-ññññð¾ð¹ññð²ð°')'
like so. One thing I like to point is that I'm using AngularJs in the development of this small project of mine not sure if it's important.