php - Unable to update column with appropriate charset -
i'm experiencing huge issue encoding , more entries go in database. when insert data, characters displayed properly. however, when try update column, characters misread 1 of these: авдвадва
. in same time, when insert new rows, data looks in phpmyadmin: Кон
.
<form action="additem.php" method="post"> <p> <label class='title' for="title"> title</label> <input type="text" name="title" id="title" class="input"/> </p> <p> <label class='description' for="description"> description</label> <textarea name="description" id="description" rows="10" cols="35"></textarea> </p> <p> <input type="submit" class='buttono' name="addentry" id="addentry" value="add new entry" /> </p> </form>
this db.php
<?php class db { public $mysql; function __construct() { $this->mysql = new mysqli('localhost', 'root', 'password', 'to-do') or die("unable connect database"); } function delete_by_id($id) { $query = "delete todo id = $id"; $result = $this->mysql->query($query) or die("there problem"); } function update_by_id($id, $description) { $query = "update todo set description = ? id = ?"; if ($stmt = $this->mysql->prepare($query)) { $stmt->bind_param('si', $description, $id); $stmt->execute(); } } }
this additem.php
<?php require 'db.php'; $db = new db(); if (isset($_post['addentry'])) { $query = "insert todo values ('', ?, ?)"; if ($stmt = $db->mysql->prepare($query)) { $stmt->bind_param('ss', $_post['title'], $_post['description']); $stmt->execute(); header('location: index.php'); } } else { die ($db->mysql->error); }
what's wrong it? tried setting charset utf-8 before query gets executed it's still same (actually seems not issue).
you must set mysql connection charset utf-8. can use $mysql->set_charset("utf8") this:
<?php class db { public $mysql; function __construct() { $this->mysql = new mysqli('localhost', 'root', 'password', 'to-do') or die("unable connect database"); if (!$this->mysql->set_charset("utf8")) { throw new exception("error loading character set utf8: " . $this->mysql->error); } } function delete_by_id($id) { $query = "delete todo id = $id"; $result = $this->mysql->query($query) or die("there problem"); } function update_by_id($id, $description) { $query = "update todo set description = ? id = ?"; if ($stmt = $this->mysql->prepare($query)) { $stmt->bind_param('si', $description, $id); $stmt->execute(); } } }
Comments
Post a Comment