【发布时间】: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