简体   繁体   English

PHP MySQL数据库问题

[英]PHP MySQL database problem

Code 1: 代码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()); } } ?>


This is the code to call the class. 这是调用该类的代码。

<?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());
?>


Code 2: 代码2:

\n<?php <?PHP\n  $q = $_GET['q']; $ q = $ _GET ['q'];\n$dbc=mysql_connect("localhost","root","") or die (mysql_error()); $ dbc = mysql_connect(“ localhost”,“ root”,“”)或死掉(mysql_error());\n  mysql_select_db('input_oop') or die (mysql_error()); mysql_select_db('input_oop')或死亡(mysql_error());\n  $sql = "INSERT INTO users set name = '".$q."'"; $ sql =“ INSERT INTO用户设置名称='”。$ q。“'”;\n  mysql_query($sql) or die (mysql_error()); mysql_query($ sql)或死掉(mysql_error());\n?> ?>\n

My Code 1 save in my database: 我的代码1保存在我的数据库中:
替代文字
Saving Multiple! 节省多个!

My Code 2 save in my database: 我的代码2保存在我的数据库中:
替代文字

What is wrong with my code 1? 我的代码1有什么问题?

Well, code 1 is open to SQL injection because you are not escaping $q. 好的,因为您没有转义$ q,所以代码1可以进行SQL注入。 As to why you get two records, that problem is not to be found in code 1 but probably in the code that calls userInput . 至于为什么要获得两条记录,这个问题不在代码1中找到,而可能在调用userInput的代码中userInput

它对所有SQL注入都是开放的,请尝试拥有db.php文件,并且在每个需要该数据库的php文件的开头都添加require_once。

Regarding SQL injection vulnerabilities, I'd suggest using prepared statements with PDO. 关于SQL注入漏洞,我建议对PDO使用预准备的语句。 It's easy to use and extremely secure. 它易于使用且极为安全。

More info: http://php.net/manual/en/pdo.prepared-statements.php 更多信息: http : //php.net/manual/zh/pdo.prepared-statements.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM