php pdo的使用
防止sql注入
$dbms='mysql'; //数据库类型 $host='localhost'; //数据库主机名 $dbName='test'; //使用的数据库 $user='root'; //数据库连接用户名 $pass='root'; //对应的密码 $dsn="$dbms:host=$host;dbname=$dbName"; try { $db = new PDO($dsn, $user, $pass); //初始化一个PDO对象 } catch (PDOException $e) { die ("Error!: " . $e->getMessage() . "<br/>"); } $username = isset($_GET['username'])?$_GET['username']:''; $ip = isset($_GET['ip'])?$_GET['ip']:''; $mac = isset($_GET['mac'])?$_GET['mac']:''; $apMac = isset($_GET['apMac'])?$_GET['apMac']:''; $loginTime = isset($_GET['loginTime'])?$_GET['loginTime']:''; $time = date('Y-m-d H:i:s'); //编写sql使用占位符 $sql = "insert into test(username,ip ,mac ,apMac ,loginTime ,insert_time) values(:username,:ip ,:mac ,:apMac ,:loginTime ,'$time')"; $query = $db->prepare($sql); /** 感觉挺麻烦的 $query->bindParam(':username',$username); $query->bindParam(':ip ',$ip ); $query->bindParam(':mac ',$mac ); $query->bindParam(':apMac ',$apMac ); $query->bindParam(':loginTime ',$loginTime ); $query->execute(); **/ $result = $query->execute(array( ':username' => $username, ':ip ' => $ip, ':mac ' => $mac, ':apMac ' => $apMac, ':loginTime ' => $loginTime, ));
csdn地址https://blog.csdn.net/xgs736214763/article/details/78849428