繁体   English   中英

php-mysql数据库撇号和逗号插入

[英]php-mysql database apostrophe and comma insertion

我要按原样从php代码在mysql中插入10个字段的值。 问题是,只要用户插入撇号和逗号(',),查询代码就会受到干扰。 有一些功能。 但是是否有必要从这些函数解析所有字段的值? 会不会很耗时:P

这是我的PHP代码

$rs = mysql_query("
    insert into 
        _{$pid}_item 
    values (
        '$pid',
        '$item_brand',
        '$item_code',
        '$item_name',
        '$item_quantity',
        '$item_mrp',
        '$i‌tem_discount',
        '$item_vat',
        '$item_sat',
        '$item_selling_price',
        '$item_rating',
        '$item‌​_image'
    )
"); 

我将值传递给这些变量。

试试类似mysql_real_escape_string东西,或者如果使用PDO ,请使用PDO::quote

并请请在SQL注入攻击阅读起来。 这不仅仅是获得失败查询的问题,还与让攻击者像其他所有用户的信息一样获得对整个数据库的访问权有关。

更好的是使用准备好的语句 看起来像这样:

<?php
//Use of $pid in the table name is strange here (see comments section) and is
// dangerous unless you're generating it yourself entirely from known information
// sources. Otherwise you definitely need to sanitize it, which I don't think
// prepared statements or quoting can do.
$stmt = $dbh->prepare("
    INSERT INTO 
        :_{$pid}_item
    VALUES (
        :pid,
        :item_brand,
        :item_code,
        :item_name,
        :item_quantity,
        :item_mrp,
        :i‌tem_discount,
        :item_vat,
        :item_sat,
        :item_selling_price,
        :item_rating,
        :item‌​_image)
"); 

$stmt->bindParam(":pid", $pid);
$stmt->bindParam(":item_brand", $item_brand);
$stmt->bindParam(":item_code", $item_code);
//... etc ...
$stmt->execute();

?>

有关您的问题的最佳完整解释,请参见此处

您可能已经注意到,如果某人能够输入任何内容并使系统崩溃,则您的代码将无法正确实现。

上面的文章中解释了避免这种情况的最佳方法。 祝您阅读愉快,并选择最适合您的情况的方法。 :)

    $query = str_replace("\'","''", $query);
    $query = stripslashes($query);

我一直在用这两个婴儿进行类似的情况。 我还没有听到抱怨。 试试看。 或玩。

使用addslashes()php函数。

http://php.net/manual/zh/function.addslashes.php

它并没有您想象的那么耗时。 没什么

有时您需要检查标题。

这不接受撇号:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

为了使单引号能更好地工作,最好在标头中使用它:

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>

暂无
暂无

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

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