1

Guys this is a code i have written. I have two files.

File one.

$regname=$_POST['name']; -----> here the variable passed is john suppose.. 
$sponserid=$_POST['sname'];   
$regemail=$_POST['email'];
$regmobile=$_POST['mobile'];
include 'dbcon.php';
$obj = new dbcon;  
$obj->createUser($regname,$sponserid,$regemail,$regmobile); 
echo $obj;

in the above code i am getting variables from a form a and storing them. Then I instantiate an object and pass all those to a method.

My class code id like this.

class dbcon
{
  public function __construct() //This is the connection construct.
{

        $server = "localhost";
        $user = "eplu";
        $pass = "123456"; //Change on hosting server
        $db = "epl";
        mysql_connect($server, $user, $pass) or die("Error connecting to sql server: ".mysql_error());
        mysql_select_db($db);
        }


public function createUser($regname,$sponserid,$regemail,$regmobile){
        $sql = "INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES (`$regname`,`$sponserid`,`$regemail`,`$regmobile`)";
        mysql_query($sql) or die(mysql_error());
        return "Registration Success";
        }

}

I get an error like Unknown column 'john' in 'field list'. New to OOPS pls help...Thnx in advance.....

MattDavey
  • 8,897
  • 3
  • 31
  • 54
DarthVader
  • 98
  • 8
  • You are using wrong type of quotes. See this question: http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks – Salman A May 29 '13 at 07:56
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško May 29 '13 at 17:29

3 Answers3

3

Try now. This is not an OOPS related error, just the database thing.

class dbcon
{
  public function __construct() //This is the connection construct.
{

        $server = "localhost";
        $user = "eplu";
        $pass = "123456"; //Change on hosting server
        $db = "epl";
        mysql_connect($server, $user, $pass) or die("Error connecting to sql server: ".mysql_error());
        mysql_select_db($db);
        }


public function createUser($regname,$sponserid,$regemail,$regmobile){
        $sql = "INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES ('$regname','$sponserid','$regemail','$regmobile')";
        mysql_query($sql) or die(mysql_error());
        return "Registration Success";
        }

}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
2

Its because you are using backticks in your SQL query values. You need to use apostrophes instead of backticks, else the query will think you are referencing another column, in this case, 'john'

Change:

 INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES (`$regname`,`$sponserid`,`$regemail`,`$regmobile`)

to:

INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES ('$regname','$sponserid','$regemail','$regmobile')
Phil Cross
  • 9,017
  • 12
  • 50
  • 84
  • I tried the quotes now i get these errors. Missing argument 1 for dbcon::createUser(), called in C:\wamp\www\backup\process\dbcon.php on line 24 and defined in C:\wamp\www\backup\process\dbcon.php on line 17 – DarthVader May 29 '13 at 08:09
  • Thats a completely different problem. I would suggest updating your original question. Also, make sure you are actually passing a value to $regname, IE make sure `$_POST['name']` isn't empty. – Phil Cross May 29 '13 at 08:13
0

Simple change

`name`

to 'name' You are using wrong quotes. This question is not really related to OOP