简体   繁体   English

我应该如何使用php在mysql数据库中插入文本?

[英]how should i insert text in mysql database using php?

How should i insert texts messages in mysql database using PHP. 我应该如何使用PHP在mysql数据库中插入短信。

My Code: 我的代码:

<?php

$message = htmlspecialchars($_POST['message']);
$message = mysqli_real_escape_string($connection, $message);

mysqli_query($connection, "INSERT INTO messages (messages) VALUE ('$message')");
?> 

My problem : 我的问题 :

When i am entering this sign (°) in message text area, it inserts nothing. 当我在消息文本区域中输入此符号(°)时,不会插入任何内容。

You should prefer prepared statements over escaping string. 与转义字符串相比,您应该更喜欢准备好的语句。

$connection = new mysqli('localhost', 'user', 'password', 'database');

if (!($stmt = $connection->prepare('INSERT INTO `messages` (`messages`) VALUES(?);')))
{
  echo "error {$connection->errno} on prepare: {$mysqli->error}";
}


if (!$stmt->bind_param('s', $message))
{
  echo "error {$stmt->errno} on bind param: {$stmt->error}";
}

if (!$stmt->execute())
{
  echo "error {$stmt->errno} on execute: {$stmt->error}";
}

尝试像这样引用值:

mysqli_query($connection, "INSERT INTO messages (messages) VALUE ('$message')");

Put this code before you execute your query 在执行查询之前放入此代码

mysqli_set_charset($con,"utf8");

and set collation of your column to 并将您的列排序规则设置为

utf8_general_ci

Use base64_encode and base64_decode 使用base64_encodebase64_decode

Here is some example. 这是一些例子。

$str = '° some special chars 😆😆😆';

$str_encoded = base64_encode($str);
echo $str_encoded; //ready to be inserted to the DB
$str_decoded = base64_decode($str_encoded);
echo  $str_decoded; // retrieved and decoded

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM