0

Possible Duplicate:
Inserting values into database using object oriented programming

I am very new to the concept of object oriented in PHP . I have been trying to insert the values of my form to the database using object oriented concept. I do not receive any error message but the values are not getting updated on the database .

Kindly take a look at the code and let me know what changes I should be doing in order to make it work.

Input.php

<html>
<body>
<form action="database.php" method="post">
Name : <input type ="text" name = "name"/>
Number  :<input type ="text" name = "number"/>
<input type ="submit" value = "submit"/>
</form>
</body>
</html>

database.php

<?php
class Database
{
    var $host;
    var $user;
    var $pass;
    var $data;
    var $con;
    var $table;
    var $db;

    public function hostname()
    {
        $this->host="localhost";
    }
    public function username()
    {
        $this->user="cgiadmin";
    }
    public function password()
    {
        $this->pass="cgi";
    }
    public function databasename()
    {
        $this->data="j2";
    }
    public function connection()
    {
        $this->con="mysql_connect($this->host,$this->user,$this->pass)";
    }
    public function tablename()
    {
        $this->table="Insert into table(name,number) values('$_POST[name]','$_POST[number]')";
    }
    public function databaseconnection()
    {
        $this->db="mysql_select_db($this->data,$this->con)";
    }
    public function mysql_close()
    {
    }
}
$name=new Database;
$name->connection();
$name->databaseconnection();
$name->tablename();

echo "thanks for taking the survey";
$name->mysql_close();
?>

Kindly let me know the changes which needs to be done . Thank you .

EDIT

I made a few changes and still it doesnt seem to worl . Please help

<?php
class Database
{
    var $host;
    var $user;
    var $pass;
    var $data;
    var $con;
    var $table;
    var $db;

    public function controls()
    {
        $this->host="localhost";
        $this->user="cgiadmin";
        $this->pass="cgi";
        $this->data="j2";
    }

    public function connection()
    {
        $this->con="mysql_connect($this->host,$this->user,$this->pass)";
    }
    public function tablename()
    {
        $this->table="Insert into employee(name,number) values('$_POST[name]','$_POST[number]')";
    }
    public function databaseconnection()
    {
        $this->db="mysql_select_db($this->data,$this->con)";
    }

}
$name=new Database;
$name->connection();
$name->databaseconnection();
$name->tablename();

echo "thanks for taking the survey";

?>
Community
  • 1
  • 1
Jathin
  • 79
  • 1
  • 1
  • 8
  • 1
    You should avoid hard coding everything in the Class. – Subir Kumar Sao Jun 14 '12 at 04:46
  • no code in mysql_close() – setzamora Jun 14 '12 at 04:47
  • your table is table, I'm not sure if it's valid. Considering it's an SQL keyword. – setzamora Jun 14 '12 at 04:49
  • 1
    @subirkumarsao as I said I am new to this programming language . So kindly help me with a sample code for the above example . Thank you – Jathin Jun 14 '12 at 04:49
  • @Joset Give me a minute . Let me just change the name of the table and try it . Thanks for your quick response . – Jathin Jun 14 '12 at 04:50
  • 2
    @Jathin: If you are just learning this language, then please don't waste any time learning the `mysql_` functions. They are insecure, and soon to be deprecated. Learn PDO. Here's an excellent tutorial : http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ – Ayush Jun 14 '12 at 04:51
  • 1
    You don't specify var for variable. Just give the access specifier and variable name. like 'public $name;' – Subir Kumar Sao Jun 14 '12 at 04:52
  • @Joset I need to start coding using object oriented concepts in php very very soon . So this is a learning curve for me – Jathin Jun 14 '12 at 05:01
  • Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Madara's Ghost Jun 14 '12 at 13:21

1 Answers1

0

Try changing

$this->table="Insert into table(name,number) values('$_POST[name]','$_POST[number]')";

To something like

$this->table="Insert into sometable(name,number) values('$_POST[name]','$_POST[number]')";

Make sure you have your database updated as well.

It looks like table is not a valid name for a table. It's an SQL keyword.

And you didn't call any mysql_db_query function or equivalent.

Edit:

mysql_db_query is deprecated. Use the alternative from the link given.

setzamora
  • 3,560
  • 6
  • 34
  • 48