Enter data in db by php

Asked

Viewed 33 times

0

I am trying to register data in the database by php, but it does not register in utf8, but when I register directly in the database it registers in uf8.

<?php
   include("conecta.php");
   function insereUsuario($conexao, $nome, $email) {
      $query = "insert into usuarios (nome, email) values ('{$nome}', '{$email}')";
      return mysqli_query($conexao, $query); 
   }
  • Post your code! So it gets hard to identify the error.

  • 3

    <?php include("connects.php"); Function inserts Your($connection, $name, $email) { $query = "Insert into users (name, email) values ('{$name}', '{$email}')"; Return mysqli_query($connection, $query); }

  • Your problem is similar to this? --> Handling UTF8 with PHP and Mysql Explain the problem better, if possible, adapt your question with examples! >>How to create a Minimal, Complete, and Verifiable example

  • try to use mysqli_set_charset($conexao, "utf8"). Documentation: http://php.net/manual/en/mysqli.set-charset.php

  • 1

    Thanks friend! It worked out here thank you very much.

1 answer

1


It would be interesting to have access to your file connects.php to be able to provide a more appropriate assistance, however, I believe that your problem can be solved by using:

mysqli_set_charset($conexao, "utf8")

That instruction would be placed within your connects.php and would receive the connection Resource as the first parameter, the second parameter will be your charset, in this case utf8. Example:

$conexao = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
mysqli_set_charset($conexao, "utf8")

Works with: (PHP 5 >= 5.0.5, PHP 7)

Documentation: http://php.net/manual/en/mysqli.set-charset.php

  • <?php $connected = mysqli_connect("localhost", "root", "", "canteen");

  • 2

    Thanks friend! It worked out here thank you very much.

  • Always the orders! It is not scope of this topic, however, I would recommend you to use PDO, I think that in addition to safer may end up being more practical.

  • You recommend some book or tutorial on the net, because I’m still learning a lot. And thanks for the tip.

  • In order not to lengthen this topic with different subjects, please contact me to talk. My contact details are public in my profile.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.