Take the 2-minute tour ×

Code 1:

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.

include ('class.php');
$user = new User;

Code 2:

  $q = $_GET['q'];
  mysql_select_db('input_oop') or die (mysql_error());
  $sql = "INSERT INTO users set name = '".$q."'";

My Code 1 save in my database:
alt text
Saving Multiple!

My Code 2 save in my database:
alt text

What is wrong with my code 1?

share|improve this question
What do you want it to do in the first place? –  Redburn Apr 27 '10 at 18:11
Besides your actual problem: Please keep in mind to validate every incoming data before sending it to the database. Otherwise your script will be vulnerable for SQL-Injections. Take a look at mysql_real_escape() for example. –  Ham Apr 27 '10 at 18:13
How are you calling the userInput() function in Code1? –  harwig Apr 27 '10 at 18:13
Can we see more code from your first example? –  Nathan Osman Apr 27 '10 at 18:14
look again. I put the code calling the userInput() function. –  Jorge Apr 27 '10 at 18:20

1 Answer 1

up vote 2 down vote accepted

Well, code 1 is open to SQL injection because you are not escaping $q. 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.

share|improve this answer

Your Answer


