How should i insert texts messages in mysql database using PHP.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.