单击“提交”按钮时,如何使用 php oops 概念将文本框值插入数据库 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19617835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:47:31  来源:igfitidea点击:

how to insert the textbox values to the database mysql using php oops concept when clicked on the Submit button

phphtmlmysql

提问by Raj

<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" name="submit"/>

</form>
</body>

</html>
<?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="root";
        $this->pass="";

        $this->data="employeedatabase";
       }

    public function connection()
    {

        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    }

    public function tablename()
    {

        $this->table=mysql_query("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->controls();

$name->connection();
if(!($name->con))

{
    echo 'Error: ' . mysql_error();

}
$name->databaseconnection();

$name->tablename();
?>

回答by Kiran Gopalakrishnan

This is the html form

这是html表单

<body>
<form action="process.php" method="post">
Name : <input type ="text" name = "Name"/>

Number  :<input type ="text" name = "Number"/>
<input type ="submit" value = "submit" name="submit"/>

</form>
</body>

This php file containing the class is named db.php

这个包含类的 php 文件名为 db.php

<?php
class db
{
    public $host;
    public $user;
    public $pass;
    public $data;
    public $con;
    public $table;
    function db()
    {
        $this->host="localhost";
        $this->user="usern";
        $this->pass="passwrd";
        $this->data="dbname";   
    }   
    public function connect()
    {
        $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();
        }
    }
    public function insert($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"

此脚本适用于您在 html 表单的“action”属性中指定的 php 文件,我将其命名为“process.php”

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

回答by Kiran Gopalakrishnan

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

        class Database
         {
    
    var $host;
    var $user;
    
    var $pass;
    var $data;
    
    var $con;
    var $table;
    
    var $db;
     function Database()
    
      {
       $this->host="localhost";
    
        $this->user="root";
        $this->pass="";
    
        $this->data="employeedatabase";
       }
    
    public function connection()
    {
    
        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if(!$this->con)
        {
        echo mysql_error();
        }
    }
    
    public function tablename()
    {
    
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST['name']."','".$_POST['number']."')");
    if(!$this->table)
        {
        echo mysql_error();
        }
    else
        {
          echo "success";
        }
    
    }
    public function databaseconnection()
    {
            $this->db=mysql_select_db($this->data,$this->con);
        if(!$this->db)
           {
    
            echo mysql_error();
           }
    
        }
    }
    
  1. 请不要在同一页面中使用类和它的对象,OOPS 的核心思想是为代码带来可重用性,那么在同一个脚本中同时使用类和它的对象有什么意义呢?将类保存在单独的 php 文件中,然后使用

    include'filename.php'
    

    然后您可以在具有类似要求的任何脚本中使用该类

  2. 不要在函数中设置类变量,在构造函数中为变量提供值,这样您就不必调用单独的函数来为类变量设置值,因为每次创建对象时都会调用构造函数

  3. 指定类变量的访问控制模式,“public”或“private”或“protected”这里是修改后的类文件,名为class.db.php

        class Database
         {
    
    var $host;
    var $user;
    
    var $pass;
    var $data;
    
    var $con;
    var $table;
    
    var $db;
     function Database()
    
      {
       $this->host="localhost";
    
        $this->user="root";
        $this->pass="";
    
        $this->data="employeedatabase";
       }
    
    public function connection()
    {
    
        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if(!$this->con)
        {
        echo mysql_error();
        }
    }
    
    public function tablename()
    {
    
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST['name']."','".$_POST['number']."')");
    if(!$this->table)
        {
        echo mysql_error();
        }
    else
        {
          echo "success";
        }
    
    }
    public function databaseconnection()
    {
            $this->db=mysql_select_db($this->data,$this->con);
        if(!$this->db)
           {
    
            echo mysql_error();
           }
    
        }
    }
    

And here's the script for the database.php

这是 database.php 的脚本

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