簡體   English   中英

我應該如何使用php在mysql數據庫中插入文本?

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

我應該如何使用PHP在mysql數據庫中插入短信。

我的代碼:

<?php

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

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

我的問題 :

當我在消息文本區域中輸入此符號(°)時,不會插入任何內容。

與轉義字符串相比,您應該更喜歡准備好的語句。

$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')");

在執行查詢之前放入此代碼

mysqli_set_charset($con,"utf8");

並將您的列排序規則設置為

utf8_general_ci

使用base64_encodebase64_decode

這是一些例子。

$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