0

我要从 php 代码中插入 10 个字段的值到 mysql 中。问题是每当用户插入撇号和逗号(',) 时,查询代码就会受到干扰。有一些功能。但是有必要从这些函数中解析所有字段的值吗?会不会很耗时: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'
    )
"); 

我将值传递给这些变量..

4

5 回答 5

4

尝试类似的东西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();

?>
于 2013-08-07T16:46:55.817 回答
2

可以在此处找到有关您的问题的最佳完整解释。

您可能已经注意到,如果有人能够输入任何内容并使您的系统崩溃,那么您的代码就没有正确实现。

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

于 2013-08-07T16:53:57.180 回答
-2
    $query = str_replace("\'","''", $query);
    $query = stripslashes($query);

我一直在使用这两个婴儿来应对类似的情况。我还没有听到抱怨。试试看。或者玩它。

于 2013-08-07T16:48:16.810 回答
-2

使用 addlashes() php 函数。

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

它并不像您想象的那么耗时。不明显。

于 2013-08-07T16:48:31.287 回答
-2

有时你需要检查你的标题。

这不接受撇号:

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

为了让您能够很好地使用撇号,最好在标题中使用它:

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
于 2013-12-13T13:41:01.727 回答