-2

users.php:

'''

class users {
    private $id;
    private $name;
    private $email;
    private $login_status;
    private $last_login;
    private $db_conn;

    function setID($id){ $this->id = $id; }
    function getID($id){ return $this->id; }
    function setName($name){ $this->name = $name; }
    function getName($name){ return $this->name; }
    function setEmail($email){ $this->email = $email; }
    function getEmail($email){ return $this->email; }
    function setLoginStatus($login_status){ $this->login_status = $login_status; }
    function getLoginStatus($login_status){ return $this->login_status; }
    function setLastLogin($last_login){ $this->last_login = $last_login; }
    function getLastLogin($last_login){ return $this->last_login; }

    public function __construct() {
        require_once("db_conn.php");
        $db = new db_conn();
        $this->db_conn = $db->connect();
    }

    public function save() {
        $sql = "INSERT INTO users ('id', 'name', 'email', 'login_status', 'last_login') VALUES (null, :name, :email, :login_status, :last_login)";
        $stmt = $this->db_conn->prepare($sql);
        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":login_status", $this->login_status);
        $stmt->bindParam(":last_login", $this->last_login);

        try{
            if($stmt->execute()){
                return true;
            }else{
                return false;
            }
        }catch(Exception $e){
            echo $e->getMessage();
        }
        }
}

'''

db_conn.php '''

class db_conn{
    private $host = 'localhost';
    private $db = 'webapp';
    private $user = 'root';
    private $pass = '';

    public function connect(){
        try{
            $conn = new PDO('mysql:host=' . $this->host . '; db=' . $this->db, $this->user, $this->pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $conn;
        }catch(PDOException $e){
            echo 'Database Error: ' . $e->getMessage();
        }
    }
}

?> '''

When I run my file, it shows error like this : I have update my code, but now it shows error like SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected.

I have search at Stackoverflow but still not find the right solution. Please help me.

2 Answers2

1

Your code must use backticks for all columns and not apostrophes

$sql = "INSERT INTO `users` (`id`, `name`, `email`, `login_status`, `last_login`)
 VALUES (null, :name, :email, :login_status, :last_login)";

For your Problem with no database selected check your connection string, if there is a Databasename .

But you can always send a sql query

use Databasename;

Or use the correct sy code instead old db= it must be dbname=

class db_conn{
    private $host = 'localhost';
    private $db = 'webapp';
    private $user = 'root';
    private $pass = '';

    public function connect(){
        try{
            $conn = new PDO('mysql:host=' . $this->host . '; dbname=' . $this->db, $this->user, $this->pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $conn;
        }catch(PDOException $e){
            echo 'Database Error: ' . $e->getMessage();
        }
    }
user3783243
  • 5,368
  • 5
  • 22
  • 41
nbk
  • 45,398
  • 8
  • 30
  • 47
0

You have single quotes arround table name users

Change this line :

$sql = "INSERT INTO 'users' ('id', 'name', 'email', 'login_status', 'last_login');

To :

$sql = "INSERT INTO users (id, name, email, login_status, last_login) VALUES (null, :name, :email, :login_status, :last_login)";