PHP MySQL数据库问题
代码1:
<?php
class dbConnect {
var $dbHost = 'localhost',
$dbUser = 'root',
$dbPass = '',
$dbName = 'input_oop',
$dbTable = 'users';
function __construct() {
$dbc = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass) or die ("Cannot connect to MySQL : " . mysql_error());
mysql_select_db($this->dbName) or die ("Database not Found : " . mysql_error());
}
}
class User extends dbConnect {
var $name;
function userInput($q) {
$sql = "INSERT INTO $this->dbTable set name = '".$q."'";
mysql_query($sql) or die (mysql_error());
}
}
?>
这是调用该类的代码。
<?php
include ('class.php');
$q=$_GET["q"];
$user = new User;
$user->userInput($q);
?>
代码2:
<?php
$q = $_GET['q'];
$dbc=mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db('input_oop') or die (mysql_error());
$sql = "INSERT INTO users set name = '".$q."'";
mysql_query($sql) or die (mysql_error());
?>
我的代码1保存在我的数据库中:
保存多个!
我的代码2保存在我的数据库中:
我的代码1有什么问题?
那么,代码1开放给SQL注入,因为你不是逃避$ q。 至于你为什么得到两个记录,这个问题不是在代码1中找到的,但可能在调用userInput的代码中。
它对SQL注入非常开放,尝试在每个需要db的php文件的开头都有一个db.php文件和require_once。
链接地址: http://www.djcxy.com/p/21865.html