Home >Backend Development >PHP Tutorial >Introduction to two methods of preventing SQL injection in PHP (code examples)

Introduction to two methods of preventing SQL injection in PHP (code examples)

不言
不言forward
2019-01-31 10:29:272727browse

This article brings you an introduction to the methods of preventing SQL injection in PHP (two types). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP simply implements methods to prevent SQL injection. It analyzes common operating techniques and precautions in PHP to prevent SQL injection in the form of examples. The PHP source code is equipped with detailed comments for easy understanding. Friends in need can refer to it!

Method 1: execute and substitute parameters


 $var_Value) {
    //获取POST数组最大值
    $num = $num + 1;
  }
  //下标为i的数组存储的是商品id, 下标为j数组的存储的是此商品的库存
  for($i=0;$isetAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      //查询数据库中是否存在该ID的商品
      //当调用 prepare() 时,查询语句已经发送给了数据库服务器,此时只有占位符 ? 发送过去,没有用户提交的数据
      $stmt = $pdo->prepare("select good_id from delphi_test_content WHERE good_id = ?");
      //当调用到 execute()时,用户提交过来的值才会传送给数据库,他们是分开传送的,两者独立的,SQL攻击者没有一点机会。
      $stmt->execute(array($_POST[$i]));
      //返回查询结果
      $count = $stmt->rowCount();
      //如果本地数据库存在该商品ID和库存记录,就更新该商品的库存
      if($count != 0)
      {
        $stmt = $pdo->prepare("update delphi_test_content set content = ? WHERE good_id = ?");
        $stmt->execute(array($_POST[$j], $_POST[$i]));
      }
      //如果本地数据库没有该商品ID和库存记录,就新增该条记录
      if($count == 0)
      {
        $stmt = $pdo->prepare("insert into delphi_test_content (good_id,content) values (?,?)");
        $stmt->execute(array($_POST[$i], $_POST[$j]));
      }
    }
  }
  $pdo = null;
  //关闭连接
}
?>

Method 2: bindParam binding parameters


$var_Value) {
    //获取POST数组最大值
    $num = $num + 1;
  }
  //下标为i的数组存储的是商品id, 下标为j数组的存储的是此商品的库存
  for($i=0;$iprepare("select good_id from delphi_test_content WHERE good_id = ?");
      $stmt->execute(array($_POST[$i]));
      $stmt->bindParam(1,$_POST[$i]);
      $stmt->execute();
      //返回查询结果
      $count = $stmt->rowCount();
      //如果本地数据库存在该商品ID和库存记录,就更新该商品的库存
      if($count != 0)
      {
        $stmt = $pdo->prepare("update delphi_test_content set content = ? WHERE good_id = ?");
        $stmt->execute(array($_POST[$j], $_POST[$i]));
        $stmt->bindParam(1,$_POST[$j]);
        $stmt->bindParam(2,$_POST[$i]);
        $stmt->execute();
      }
      //如果本地数据库没有该商品ID和库存记录,就新增该条记录
      if($count == 0)
      {
        $stmt = $pdo->prepare("insert into delphi_test_content (good_id,content) values (?,?)");
        $stmt->bindParam(1,$_POST[$i]);
        $stmt->bindParam(2,$_POST[$j]);
        $stmt->execute();
      }
    }
  }
  $pdo = null;
  //关闭连接
}
?>

The above is the detailed content of Introduction to two methods of preventing SQL injection in PHP (code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete