8
To store form data in sessions using php classes and objects
source link: https://www.codesd.com/item/to-store-form-data-in-sessions-using-php-classes-and-objects.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
To store form data in sessions using php classes and objects
advertisements
I have class which is used to insert the 'id' in the table using PDO connection and now i have to store form data i.e id in session variable .
<?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();
?>
How to store session variable in table how it will be achieved .
Why not just pass the $_SESSION['campid']
value to insert_info()
method?
$_SESSION['campid']='camp1001';
$db = new table1(); // it is used to object of class table1.
$res=$db->insert_info($_SESSION['campid']);
and the function
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;
}
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK