<?php
//==========================================================================
class Database {
private $host = "127.0.0.1";
private $database_name = "phpapidb";
private $username = "root";
private $password = "xxxxxxxx";
public $conn;
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Database could not be connected: " . $exception->getMessage();
}
return $this->conn;
}
}
//=====================================================================================
class userAccount{
// Connection
private $conn;
// Table
private $db_table = "userAccount";
// Columns
public $id;
public $name;
public $email;
public $age;
public $designation;
public $created;
// Db connection
public function __construct($db){
$this->conn = $db;
}
// GET ALL
public function getuserAccount(){
$sqlQuery = "SELECT id, name, email, age, designation, created FROM " . $this->db_table . "";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
return $stmt;
}
// CREATE
public function createuserAccount(){
$sqlQuery = "INSERT INTO
". $this->db_table ."
SET
name = :name,
email = :email,
age = :age,
designation = :designation,
created = :created";
$stmt = $this->conn->prepare($sqlQuery);
// sanitize
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->age=htmlspecialchars(strip_tags($this->age));
$this->designation=htmlspecialchars(strip_tags($this->designation));
$this->created=htmlspecialchars(strip_tags($this->created));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":age", $this->age);
$stmt->bindParam(":designation", $this->designation);
$stmt->bindParam(":created", $this->created);
if($stmt->execute()){
return true;
}
return false;
}
// READ single
public function getSingleuserAccount(){
$sqlQuery = "SELECT
id,
name,
email,
age,
designation,
created
FROM
". $this->db_table ."
WHERE
id = ?
LIMIT 0,1";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->bindParam(1, $this->id);
$stmt->execute();
$dataRow = $stmt->fetch(PDO::FETCH_ASSOC);
$this->name = $dataRow['name'];
$this->email = $dataRow['email'];
$this->age = $dataRow['age'];
$this->designation = $dataRow['designation'];
$this->created = $dataRow['created'];
}
// UPDATE
public function updateuserAccount(){
$sqlQuery = "UPDATE
". $this->db_table ."
SET
name = :name,
email = :email,
age = :age,
designation = :designation,
created = :created
WHERE
id = :id";
$stmt = $this->conn->prepare($sqlQuery);
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->age=htmlspecialchars(strip_tags($this->age));
$this->designation=htmlspecialchars(strip_tags($this->designation));
$this->created=htmlspecialchars(strip_tags($this->created));
$this->id=htmlspecialchars(strip_tags($this->id));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":age", $this->age);
$stmt->bindParam(":designation", $this->designation);
$stmt->bindParam(":created", $this->created);
$stmt->bindParam(":id", $this->id);
if($stmt->execute()){
return true;
}
return false;
}
// DELETE
function deleteuserAccount(){
$sqlQuery = "DELETE FROM " . $this->db_table . " WHERE id = ?";
$stmt = $this->conn->prepare($sqlQuery);
$this->id=htmlspecialchars(strip_tags($this->id));
$stmt->bindParam(1, $this->id);
if($stmt->execute()){
return true;
}
return false;
}
}
<?php
class Tour{
// Connection
private $conn;
// Table
private $db_table = "tour";
//Database Connection
function __construct(){
$database = new Database();
$db = $database->getConnection();
$this ->conn= $db;
}
// CREATE TOUR
public function createNewTour($tourName){
//$sqlQuery="INSERT INTO". $this->db_table ."VALUES(:TID,:tourName,:tourType,:child,:price,:NoDiscount,:discount,:childPrice,:duration,:description,:imgID,:TiID,:status) ";
$sqlQuery="INSERT INTO". $this->db_table ."VALUES(:TID,:tourName) ";
try {
$stmt = $this->conn->prepare($sqlQuery);
// sanitize
$this->TID=1;
$this->tourName=EncryptInput($this->conn,$tourName);
// $this->tourType=EncryptInput($this->conn,$tourType);
// $this->child=EncryptInput($this->conn,$child);
// $this->price=EncryptInput($this->conn,$price);
// $this->NoDiscount=EncryptInput($this->conn,$NoDiscount);
// $this->discount=EncryptInput($this->conn,$discount);
// $this->childPrice=EncryptInput($this->conn,$childPrice);
// $this->duration=EncryptInput($this->conn,$duration);
// $this->description=EncryptInput($this->conn,$description);
// $this->imgID=EncryptInput($this->conn,$imgID);
// $this->TiID=EncryptInput($this->conn,$TiID);
// $this->status=EncryptInput($this->conn,$status);
// bind data
$stmt->bindParam(":TID", $this->TID);
$stmt->bindParam(":tourName", $this->tourName);
// $stmt->bindParam(":tourType", $this->tourType);
// $stmt->bindParam(":child", $this->child);
// $stmt->bindParam(":price", $this->price);
// $stmt->bindParam(":NoDiscount", $this->NoDiscount);
// $stmt->bindParam(":discount", $this->discount);
// $stmt->bindParam(":childPrice", $this->childPrice);
// $stmt->bindParam(":duration", $this->duration);
// $stmt->bindParam(":description", $this->description);
// $stmt->bindParam(":imgID", $this->imgID);
// $stmt->bindParam(":TiID", $this->TiID);
// $stmt->bindParam(":status", $this->status);
if($stmt->execute()){
//return true;
//echo mysqli_error($conn);
echo 'hahah';
}
//return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
//======
//======= how to get the return from creatTour
//--1-- $tour = new Tour(); create an object of class
//--2-- $t=$tour->creatTour('city','Land Tour','no',300,2,10,10,'2hrs','this is a city tour',2,2,'published') pass parameters to method of the class;
//--3-- if($t==true){ check if return is true
// echo $t['last_id']; to get the value of last id from array
//echo json_encode($t); convert the array to json object for rest API
//--4-- }; close the if statement
//var_dump($tour);
// $this->name = $dataRow['name'];
// $this->email = $dataRow['email'];
// $this->age = $dataRow['age'];
// $this->designation = $dataRow['designation'];
// $this->created = $dataRow['created'];
// For testing
$uid = 1;
// create the sql for qd_user_usam table
$sql = "SELECT u.user_id, a.acl_id, g.group_name, e.entry_level, p.permit_level
FROM qd_users as u, qd_users_acl as a, qd_users_group as
g, qd_users_entry as e, qd_users_permission as p
WHERE u.user_id = ?
AND a.acl_id = u.user_id
AND g.group_id = a.group_id
AND e.entry_id = a.entry_id
AND p.permit_id = a.permit_id";
$_stmt = $this->_dbConn->prepare($sql);
$_stmt->execute(array($uid));
// $t = new Tour();
// $r= $t->creatTour($tourName,$tourType,$child,$price,$NoDiscount,$discount,$childPrice,$duration,$description,1,1,1,0);
// var_dump($t);
// if($r==true){
// echo $r['last_id'];
// echo json_encode($r);
// }else{
// echo 'false';
// };
You can use bindParam() in single line by this way:
$csql=$cto->prepare("INSERT INTO `users`(`username`, `password`, `class`, `is_on`, `time_log`, `IP`)
VALUES (:name,:pass,:class,0,0,'0')");
//Looping for all values into array...
foreach ($pr as $key => &$val) {
$csql->bindParam($key, $val);
}
$csql->execute();
}
?>
?>