1

Iam using this php form to insert one "name" into my PHPMyadmin database but some characters are not coming correctly for eg: tî ă jî șî pî.

Please check my codes

My Php form used to insert value into database

   <?php 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "testapps";
// Create connection
$con=mysqli_connect($servername,$username,$password,$dbname);
if(isset($_POST["delete"]))
{
    $id=$_POST['delete'];
    $rslt=mysqli_query($con,"select * from offers where id='".$id."'");
    $row=mysqli_fetch_assoc($rslt);
    $url=$row["url"];
    if(!empty($url))
        unlink($url);
    mysqli_query($con,"delete from offers where id='".$id."'");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>French Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <link rel="stylesheet" href="css/bootstrap-datetimepicker.min.css">
  <script src="js/bootstrap-datetimepicker.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<style>
.container{
 background-color: lightgrey;
width: 80%;
margin-top: 4%;
  padding-bottom: 3%;
}
.container h2{
  font-size: 16px;
}

</style>
</head>
<body>
<!-- 1-->
<div class="container">
  <h2>French lang</h2>
  <hr>
  <form role="form" action="http://54.149.175.66/allapps/php/insert.php" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <div class="form-group">
      <label for="offername"> Name:</label>
      <input type="text" class="form-control" name="offername" id="offername" placeholder="Enter Offer Name">
    </div>

    <button type="submit" class="btn btn-default">Done</button>
  </form>
  <hr>
</div>
</body>
</html>

My PHP insert query code insert.php

<?php
header("Content-type: text/html; charset=utf-8"); 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "testapps";
$charset="UTF8";
$offerName=$_POST['offername'];

echo $offerName;


$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "test";
$db_found = mysql_select_db($database, $conn);
if ($db_found) {
  echo "Db found";
}else{
  echo "No db";
}
echo $offerName;

mysqli_query("SET NAMES 'utf8'");
mysqli_query('SET CHARACTER SET utf8');
$sql = "INSERT INTO offers( `name`)  VALUES
 ('$offerName')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}




?>

The words i tried to insert are following

1.hoteluri și 2.ăâîşţ

and my database showing it like this

Pls check screenshot.enter image description here

Table collation enter image description here

Bangalore
  • 1,572
  • 4
  • 20
  • 50

1 Answers1

3

These require DB connection be passed:

mysqli_query("SET NAMES 'utf8'");
mysqli_query('SET CHARACTER SET utf8');

Read the manual:

From the manual:

Object oriented style

bool mysqli::set_charset ( string $charset )

Procedural style

bool mysqli_set_charset ( mysqli $link , string $charset )

and you don't need this

$db_found = mysql_select_db($database, $conn);

It doesn't work with mysqli_. Plus, you already declared it.


As noted, your code is open to SQL injection. Best to use a prepared statement.

Consult the following:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • How could you find these so fast?? – Bangalore Sep 24 '15 at 18:27
  • 1
    @Bangalore I type fast ;-) oh, and I had 3 coffees already. Good thing they weren't "espressos" though, otherwise I'd be walking on the ceiling. – Funk Forty Niner Sep 24 '15 at 18:28
  • 2
    One additional note. `mysqli_query($con,"delete from offers where id='".$id."'");` opens you to SQL injections. Anyone that can access the processing page could delete all records. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – chris85 Sep 24 '15 at 18:29
  • cool fred :-) just one doubt if i want to list out all value in db over that php form i can use normal select query ryt. – Bangalore Sep 24 '15 at 18:29
  • great @chris85 will check that too – Bangalore Sep 24 '15 at 18:30
  • @Bangalore Have a look at this great article on Stack http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php - and yes, it does leave you open to injection. Best to use a prepared statement, and Google XSS injection also, it's a good read. – Funk Forty Niner Sep 24 '15 at 18:30
  • 1
    @chris85 Thanks for that. I've mentioned that to Bangalore here. Cheers – Funk Forty Niner Sep 24 '15 at 18:31