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";

?>
9
  • 1
    You should avoid hard coding everything in the Class. Commented Jun 14, 2012 at 4:46
  • no code in mysql_close() Commented Jun 14, 2012 at 4:47
  • 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 Commented Jun 14, 2012 at 4:49
  • 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 : net.tutsplus.com/tutorials/php/… Commented Jun 14, 2012 at 4:51
  • 1
    You don't specify var for variable. Just give the access specifier and variable name. like 'public $name;' Commented Jun 14, 2012 at 4:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I did make the change but still it does not take the values . However I do not get any error message as well .
call mysql_db_query passing the string value returned from the tablename() function
Would you be kind enough to show me how it is done ?
check Mohit Bumb's answer. mysql_db_query is deprecated.
Hmmm Mohit bumb's answer does not seem to work . Could you take a look into the error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.