我正在使用此 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 şi 2.ăâîşţ
我的数据库是这样显示的
最佳答案
这些需要传递数据库连接:
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 注入(inject)开放。最好使用准备好的语句。
请参阅以下内容:
关于php - PHPMyadmin "tî ă jî șî pî"中的罗马尼亚语单词不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768220/