【问题标题】:to store form data in sessions using classes and objects php使用类和对象 php 在会话中存储表单数据
【发布时间】:2016-07-25 06:24:44
【问题描述】:

我有一个类,用于使用 PDO 连接在表中插入“id”,现在我必须存储表单数据,即会话变量中的 id。

    <?php
    session_start(); 
    // this class is used to create connection with database
    class Database
    {
        private $db_host = ‘localhost’; 
        private $db_user = ‘root’; 
        private $db_pass = ‘root’; 
        private $db_name = ‘test’;   


        public function connect()   {               
          $db = new PDO('mysql:host=$db_host;dbname=$db_name;charset=utf8mb4', '$db_user', '$db_pass');           
          }
    }


  this class is used to insert the id in the table 

    class table1 extends Database
    {
           public function insert_info() 
            {
                    $sql = "insert into info(id) values ('?')";
                    $sql->bind_param("s", $id);
                    $sql->execute();
                    return true;
            }
    }

    $_SESSION['campid']='camp1001';
    $db = new table1();       // it is used to object of class table1.
    $res=$db->insert_info();

    ?>

如何将会话变量存储在表中将如何实现。

【问题讨论】:

    标签: php oop pdo


    【解决方案1】:

    将会话传递给方法。就像将变量传递给函数一样。

    $_SESSION['campid']='camp1001';
    $db = new table1();       // it is used to object of class table1.
    $res=$db->insert_info($_SESSION['campid']);
    
    class table1 extends Database
        {
               public function insert_info($id) 
                {
                        $sql = "insert into info(id) values ('?')";
                        $sql->bind_param("s", $id);
                        $sql->execute();
                        return true;
                }
        }
    

    或将会话值传递给构造函数。这会变长,但在某些情况下会用到。

    $db = new table1($_SESSION['campid']);
    $res=$db->insert_info();
    
    class table1 extends Database
        {
            public $id;   
                __construct($id){
                    $this->id = $id;
                }
                public function insert_info() 
                {
                        $sql = "insert into info(id) values ('?')";
                        $sql->bind_param("s", $this->id);
                        $sql->execute();
                        return true;
                }
        }
    

    【讨论】:

    • 在construct() 或insert_info() 中使用$id 取决于模型的用途。当模型用于镜像表时,我认为参数应该包含在方法调用中。当模型表示数据库表中的记录(行)时,则在构造函数中使用$id。
    【解决方案2】:

    为什么不直接将$_SESSION['campid'] 值传递给insert_info() 方法?

    $_SESSION['campid']='camp1001';
    $db = new table1();       // it is used to object of class table1.
    $res=$db->insert_info($_SESSION['campid']);
    

    和功能

    class table1 extends Database
    {
           public function insert_info($id) // <-- add function param
            {
                    $sql = "insert into info(id) values ('?')";
                    $sql->bind_param("s", $id);
                    $sql->execute();
                    return true;
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 1970-01-01
      • 2013-06-10
      • 2017-12-06
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多