Skip to content Skip to sidebar Skip to footer

How To Insert The Textbox Values To The Database Mysql Using Php Oops Concept When Clicked On The Submit Button

Name : Number : &

Solution 1:

This is the html form

<body><formaction="process.php"method="post">
Name : <inputtype ="text"name = "Name"/>

Number  :<inputtype ="text"name = "Number"/><inputtype ="submit"value = "submit"name="submit"/></form></body>

This php file containing the class is named db.php

<?phpclassdb{
    public$host;
    public$user;
    public$pass;
    public$data;
    public$con;
    public$table;
    functiondb()
    {
        $this->host="localhost";
        $this->user="usern";
        $this->pass="passwrd";
        $this->data="dbname";   
    }   
    publicfunctionconnect()
    {
        $this->con=mysql_connect($this->host,$this->user,$this->pass);
        if(!$this->con)
        {
            echo mysql_error();
        }
        $sel=mysql_select_db($this->data, $this->con);
        if(!$sel)
        {
            echo mysql_error();
        }
    }
    publicfunctioninsert($name,$number)
    {
        $sql=mysql_query("INSERT INTO tablename(name, number) VALUES('$name', '$number')");
        if(!$sql)
        {
            echo mysql_error();
        }
    }
}
?>

This script is for the php file which you specify in the "action" attribute of your html form i have named it "process.php"

<?phpinclude'db.php';
    $name=$_POST['Name'];
    $num=$_POST['Number'];
    $n=new db();
    $n->connect();
    $n->insert($name,$num);
?>

Solution 2:

Before answering your question(i don't see any question here) i would like to point out some facts,

  1. Please don't use class and it's object in the same page,the very idea of OOPS is to bring reusability to the code so what is the point of using both the class and it's object in the same script ? save the class in a seperate php file and then include the class in your php script using

    include'filename.php'

    then you can use the class in any script with similar requirments

  2. Don't set the class variables in a function , provide values to the variables in a constructor ,so that you wouldn't have to call a seperate function for setting values to class variables ,as contructor function is invoked everytime an object is created

  3. specify the access control modes for the class variables,either "public" or "private"or "protected" here's the modified class file , named class.db.php

    classDatabase{
    
    var$host;
    var$user;
    
    var$pass;
    var$data;
    
    var$con;
    var$table;
    
    var$db;
     functionDatabase()
    
      {
       $this->host="localhost";
    
        $this->user="root";
        $this->pass="";
    
        $this->data="employeedatabase";
       }
    
    publicfunctionconnection()
    {
    
        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if(!$this->con)
        {
        echo mysql_error();
        }
    }
    
    publicfunctiontablename()
    {
    
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST['name']."','".$_POST['number']."')");
    if(!$this->table)
        {
        echo mysql_error();
        }
    else
        {
          echo"success";
        }
    
    }
    publicfunctiondatabaseconnection()
    {
            $this->db=mysql_select_db($this->data,$this->con);
        if(!$this->db)
           {
    
            echo mysql_error();
           }
    
        }
    }
    

And here's the script for the database.php

<?phpinclude'class.db.php';
$name=new Database();
$name->connection();
$name->databaseconnection();
$name->tablename();
?>

Post a Comment for "How To Insert The Textbox Values To The Database Mysql Using Php Oops Concept When Clicked On The Submit Button"