0

Below is my mysql table.

CREATE TABLE `mydb`.`mytable` ( `id` INT NOT NULL AUTO_INCREMENT, `comment` VARCHAR(100) NOT NULL , PRIMARY KEY (`id`)) ENGINE = MyISAM;

ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Below is my php code.

<?php
    if(isset($_POST["submit"])){
        $con = mysqli_connect("localhost","root","","mydb");
        $comment = mysqli_real_escape_string($con, $_POST['comment']);
        $sql="INSERT INTO `user`(`comment`) VALUES ('".$comment."')";
        if (mysqli_query($con, $sql)) {
            echo "New record created successfully";
          } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
          }
    }
?>
<html>
    <head></head>
    <body>
        <form method="POST">
            <input type="text" name="comment">
            <input type="submit" value="submit" name="submit">
        </form>
    </body>
</html>

when I try to submit with emojies in localhost, unknown characters were stored (inserted) in localhost. when I try to submit with emojies in 000webhost.com, no records were inserted to the database.

But I can store emojies directly in both database(localhost & 000webhost.com) by executing following sql command directly in mysql server(PHP MYADMIN).

INSERT INTO `mytable`(`comment`) VALUES ('');

But I couldn't insert emojies using PHP. So my question is how to insert emojies using PHP to mysql server?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohamed Arshad
  • 89
  • 1
  • 1
  • 6
  • What is the character encoding of your PHP script itself, and with what charset are you serving it to the browser? Sounds like what your form sends, might not be properly encoded as UTF-8 to begin with. – CBroe Jan 13 '21 at 08:30
  • FYI, your stuff is open to SQL injection. You should really rather be using prepared statements. – CBroe Jan 13 '21 at 08:31
  • see https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-mysql-database – Eden Moshe Jan 13 '21 at 08:57
  • 1
    Set the correct charset for the connection and use parameter binding. It will solve all your problems. – Dharman Jan 13 '21 at 12:19

0 Answers0