duanhui5344 2014-10-10 06:03
浏览 25

什么是PDO,它与SQL注入有什么关系,为什么我应该使用它?

Actually I did google and got so many results, but I can't understand, because I'm new in this field.

So what is an easy way that what is PDO, why I should use this, what is SQL injection, etc. with an example?1

Actually now my code is like that.

config.php

<?php
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "testdb";
    $prefix = "";
    $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
    mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

insert.php

<?php
    include('config.php');
    $account_no = $_POST['account_no'];
    $amount = $_POST['amount'];
    $save = mysql_query("INSERT INTO tableamount (account_no, amount) VALUES ('$account_no', '$amount',)");
    header("location: index.html");
    exit();
?>

index.html

<html>
    <body>
        <form action="amount.php" method="post" enctype="multipart/form-data" name="addroom">
            Account Number<br />
            <input name="account_no" type="text"/><br />

            Amount<br />
            <input name="amount" type="text"/><br />

            <input type="submit" name="Submit" value="Submit" id="button1" />
        </form>
    </body>
</html>
  • 写回答

2条回答 默认 最新

  • duanque19820925 2014-10-10 06:15
    关注

    Simply imagine this user input: "1'); TRUNCATE TABLE accounts; --", with your statement, if the user know what db structure you have, can easily drop everything from the db (assuming the db user have the authorizations.

    Never use the user input directly in a sql query as you've done, always escape/cast before use.

    PDO - PHP Data Objects - is a database access layer providing a uniform method of access to multiple databases.

    It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.

    Please read this link carefully, it explains why pdo should be used in php

    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab