0

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.

Phiter
  • 14,570
  • 14
  • 50
  • 84
user1915308
  • 354
  • 4
  • 12
  • You should set the code to a UTF8 charset wherever possible. PHP header: `header('Content-Type: text/html; charset=utf-8');` -- SQL connection: `mysqli_set_charset($con, "utf8");` and perhaps a bunch of other places too. The file itself and database-tables too. Also, collation is not the same as charset. – Qirel Jun 20 '16 at 15:21
  • I have a set in my HTML – user1915308 Jun 20 '16 at 15:22
  • PHP has nothing to do with HTML, but that headers specifies the HTML header (which is a good thing). You need to specify the charset in PHP too - in fact, you need to specify it wherever you can (PHP headers, connection, file, database, etc). – Qirel Jun 20 '16 at 15:23
  • Yeah but the file I'm using to send the data is just a template that holds only this particular form – user1915308 Jun 20 '16 at 15:26
  • And I'm telling you that you need to set those PHP headers, specify the connection charset and the databases! ;-) – Qirel Jun 20 '16 at 15:33

0 Answers0