0

this is my first php code and it is full of problems probably :)
I want to register information of people visiting my website. My main problem is that FARSI words in sql are not registered right and it is showing wrong characters. I tried to use N for making it unicode but it didn't work for me. Maybe I'm using it wrong. Also I tried give everyone a unique ID but I could not figure how. My sql collation for FARSI parts are utf8 persian ci. I am using phpmy admin and I tried to make nvarchar in it but it only offers varchar.
This is my first question and first php code so please forgive me If it is too basic.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?php
$name = $_POST["name"];
$familyname = $_POST["familyname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$pass = $_POST["pass"];
$pass2 = $_POST["pass2"];
$id = 100;

// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
echo("$email صحیح نمی باشد");
} else {


$con = mysqli_connect("localhost","","","register");

$name = mysqli_real_escape_string($con, $_POST['name']);
$familyname = mysqli_real_escape_string($con, $_POST['familyname']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$pass = mysqli_real_escape_string($con, $_POST['pass']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);



mysqli_query($con,"INSERT INTO users (`name17`, `familyname17`, `email17`, `phone17`, `pass17`, `id17`) VALUES ('$name', '$familyname', '$email', '$phone', '$pass', '$id')");

$result = mysqli_query($con,"select * from users where email17 ='$email' and pass17 = '$pass' and familyname17 = '$familyname'" )
    or die("faild to query database".mysql_error());
$row = mysqli_fetch_array($result,MYSQLI_BOTH);

if (empty($name) || empty($familyname) || empty($email) || empty($phone) || empty($pass) || empty($pass2)) {
echo "لطفا اطلاعات را بطور کامل وارد فرمایید";
}
 else {
if ($row['email17']==$email && $row['pass17'] == $pass2){
echo "<body style='direction: rtl !important;'>";
echo "<b>".$row['name17']." <b>";
echo "عزیز. خوش آمدید<br> ";
echo '<img src="images/takhfif.jpg" alt="تخفیف" />'; 
} else {
echo "خطا در ثبت نام.لطفا دوباره تلاش کنید";
}
}

}



?>
</body>

</html>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Noshad70
  • 35
  • 5
  • try utf8_general_ci – Shan Feb 20 '17 at 07:24
  • Question is already answered here http://stackoverflow.com/questions/6642755/insert-a-persian-text-in-mysql-table – Manikandan Feb 20 '17 at 07:38
  • Possible duplicate of [Insert a persian text in mysql table](http://stackoverflow.com/questions/6642755/insert-a-persian-text-in-mysql-table) – carmine Feb 20 '17 at 08:18
  • This is not a duplicate and other answers did not work for me. utf8_general_ci also does not work here unfortunatly – Noshad70 Feb 21 '17 at 05:49

2 Answers2

0

make sure you are using utf8 .
change your database collation to utf8_general . this should work .
I had the same problem when i was learning php .

Saeed Karimi
  • 35
  • 3
  • 8
0

Hello everyone and thanks for the answers. if found the solution to my problem. for those who have the same problem, Set collation to utf8 general or unicode. But you must set your input to utf8 also. I inserted the following code to do so. I hope this helps and if you had problemsplease ask. I use mysqli but normal mysql connection is pretty much the same.
$con = mysqli_connect("localhost","db_user","db_pass","db_name"); after connecting to db use this code: mysqli_set_charset($con,"utf8");

Noshad70
  • 35
  • 5