【问题标题】:Romanian word not coming correctly in PHPMyadmin "tî ă jî șî pî"?PHPMyadmin 中的罗马尼亚语单词“tî ă jî șî pî”不正确?
【发布时间】:2015-09-24 18:15:06
【问题描述】:

我正在使用这个 php 表单将一个“名称”插入到我的 PHPMyadmin 数据库中,但某些字符无法正确输入,例如:tî ă jî șî pî。

请检查我的代码

我的 PHP 表单用于将值插入数据库

   <?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>

我的PHP插入查询代码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;
}




?>

我尝试插入的单词如下

1.hoteluri 西 2.ăaîşţ

我的数据库是这样显示的

请查看截图。

表格排序规则

【问题讨论】:

  • 请更改编码类型
  • 喜欢吗?如何 ?你能解释一下吗?
  • 旁注:你不能使用这个有两个原因$db_found = mysql_select_db($database, $conn);
  • 那么所有mysqli_query("SET ... 都让你失望了。 RTM php.net/manual/en/mysqli.set-charset.php 并在“程序样式”下查看。
  • 你目前使用的排序规则是什么?

标签: php mysql utf-8 insert phpmyadmin


【解决方案1】:

这些需要通过数据库连接:

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

阅读手册:

来自手册:

面向对象的风格

bool mysqli::set_charset (string $charset)

程序风格

bool mysqli_set_charset (mysqli $link, string $charset)

你不需要这个

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

它不适用于mysqli_。另外,您已经声明了它。


如上所述,您的代码对 SQL 注入是开放的。最好使用准备好的语句。

请咨询以下内容:

【讨论】:

  • 你怎么能这么快找到这些??
  • @Bangalore 我打字很快 ;-) 哦,我已经喝了 3 杯咖啡了。幸好它们不是“浓缩咖啡”,否则我会走在天花板上。
  • 附加说明。 mysqli_query($con,"delete from offers where id='".$id."'"); 向您打开 SQL 注入。任何可以访问处理页面的人都可以删除所有记录。 stackoverflow.com/questions/60174/…
  • cool fred :-) 只是一个疑问,如果我想在 db 中列出该 php 表单中的所有值,我可以使用普通的选择查询 ryt。
  • @chris85 谢谢。我在这里向班加罗尔提到了这一点。干杯
猜你喜欢
  • 2021-06-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
相关资源
最近更新 更多