mysqli中未更新带撇号的字符串 [英] String with an apostrophe not being updated in mysqli

查看:59
本文介绍了mysqli中未更新带撇号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用准备好的语句来执行更新.但是由于某种原因,包含撇号的字符串不会得到更新.有没有一种方法可以更新字符串并保留撇号?

I am using prepared statements to preform an update. But for some reason a string that contains an apostrophe does not get updated. Is there a way where I can update a string ans preserve the apostrophe??

下面是我的查询示例

$query = "UPDATE items SET item_name = ? , item_price = ? WHERE id = ? ";
    $stmt = $db->prepare($query);

    foreach($items_and_prices as $id => $item_and_price){
        $item = $item_and_price[0];
        $price = $item_and_price[1];
        $stmt->bind_param("sdi",$item, $price,$id);
        $stmt->execute();

    }

推荐答案

不能正确插入字符串的原因是因为撇号是特殊字符.将特殊字符插入mysql数据库的最佳方法是先以某种方式对其进行转义.使用PHP,您有许多不同的选择.

The reason that your strings aren't inserting correctly is because apostrophes are special characters. The best way to go about inserting special characters into mysql databases, is to escape them in some way first. With PHP, you have quite a few different options.

我将在这里解释其中的一些内容

I will explain some of them here:

  • htmlentities(); Converts special characters to html entities.
    Usage: http://php.net/manual/en/function.htmlentities.php

htmlspecialchars();将特殊字符转换为html特殊字符代码(即: ) 用法: http://php.net/manual/en/function.htmlspecialchars.php

htmlspecialchars(); Converts special characters to html special character codes (ie:  ) Usage: http://php.net/manual/en/function.htmlspecialchars.php

addslashes();用反斜杠转义特殊字符\
用法: http://php.net/manual/en/function.addslashes.php

addslashes(); Escapes special characters with the backslash \
Usage: http://php.net/manual/en/function.addslashes.php

示例:$safestring = addslashes($orginalstring);将导致所有特殊字符都用\转义,导致'变为\',从而可以在数据库中插入特殊字符.

Example: $safestring = addslashes($orginalstring); would cause all special characters to be escaped with a \, causing ' to become \', making it possible to insert special characters into the database.

在输出已被addslashes();

这篇关于mysqli中未更新带撇号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆