When the language string is "en" I get this error:
Fatal error: Cannot redeclare getSetting() (previously declared in /Users/manuel/Sites/WebApply/datamanager.php:2) in /Users/manuel/Sites/WebApply/datamanager.php on line 10
I have already tried to change the require imports to require_once but without success.
index.php
if(!file_exists("mysql.php")){
header("Location: setup/index.php");
exit;
}
require_once("datamanager.php");
require_once('assets/languages/lang_'.getSetting("lang").'.php');
datamanager.php
<?php
function getSetting($setting){
require_once("mysql.php");
$stmt = $mysql->prepare("SELECT * FROM settings WHERE NAME = :setting");
$stmt->bindParam(":setting", $setting, PDO::PARAM_STR);
$stmt->execute();
while ($row = $stmt->fetch()) {
return $row["VALUE"];
}
}
function setSetting($setting, $value){
require_once("mysql.php");
$stmt = $mysql->prepare("UPDATE settings SET VALUE = :value WHERE NAME = :setting");
$stmt->bindParam(":setting", $setting, PDO::PARAM_STR);
$stmt->bindParam(":value", $value, PDO::PARAM_STR);
$stmt->execute();
}
function getRankID($username){
require_once("mysql.php");
$stmt = $mysql->prepare("SELECT * FROM accounts WHERE USERNAME = :user");
$stmt->bindParam(":user", $username, PDO::PARAM_STR);
$stmt->execute();
while ($row = $stmt->fetch()) {
return $row["USERRANK"];
}
}
?>
Related
I have a php file called "purchases.controller.php" in which within a function called 'ctrCash' of the 'Purchases' class, I pass variables to a function called 'ctrNewCashPurchase' of the 'CartController' class that I have defined, but when I run the project, I get the message:
"Fatal error : Uncaught Error: Class 'CartModel' not found in ... "
If I do a var_dump inside the function ctrNewCashPurchase, I realize that I am entering that function, but it tells me that it does not recognize 'CartModel' and I do not understand why.
I share the code of the "purchases.controller.php" file:
class CartController{
static public function ctrNewCashPurchase($datos){
$tabla = "compras";
$respuesta = CartModel::mdlNewCashPurchase($tabla, $datos);
if($respuesta == "ok"){
$tabla = "comentarios";
ModeloUsuarios::mdlIngresoComentarios($tabla, $datos);
}
return $respuesta;
}
}
class Purchases {
public function ctrCash (&$arrayCompleto, &$usuario, &$direccion1, &$direccion2, &$dia, &$hora, &$email, &$telefono, &$sesion){
if(isset($usuario)){
//Here I create an array
for($i = 0; $i < count($arrayCompleto); $i++){
$datos = array("idUsuario"=> $sesion,
"idProducto"=> $arrayCompleto[$i]["idProducto"],
"metodo"=> "Efectivo",
"email"=> $email,
"direccion"=> $direccion1,
"detalleDireccion"=> $direccion2,
"diaEnvio"=> $dia,
"horaEnvio"=> $hora,
"telefono"=> $telefono,
"pais"=> "ARG");
}
$respuesta = CartController::ctrNewCashPurchase($datos);
}
}
}
I share the code of the "purchases.model.php" file, where I define the CartModel class:
class CartModel{
static public function mdlNewCashPurchase($tabla, $datos){
$stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (id_usuario, id_producto, metodo, email, direccion, pais, detalleDireccion, diaEnvio, horaEnvio, telefono) VALUES (:id_usuario, :id_producto, :metodo, :email, :direccion, :pais, :detalleDireccion, :diaEnvio, :horaEnvio, :telefono)");
$stmt->bindParam(":id_usuario", $datos["idUsuario"], PDO::PARAM_INT);
$stmt->bindParam(":id_producto", $datos["idProducto"], PDO::PARAM_INT);
$stmt->bindParam(":metodo", $datos["metodo"], PDO::PARAM_STR);
$stmt->bindParam(":email", $datos["email"], PDO::PARAM_STR);
$stmt->bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);
$stmt->bindParam(":pais", $datos["pais"], PDO::PARAM_STR);
$stmt->bindParam(":detalleDireccion", $datos["detalleDireccion"], PDO::PARAM_STR);
$stmt->bindParam(":diaEnvio", $datos["diaEnvio"], PDO::PARAM_STR);
$stmt->bindParam(":horaEnvio", $datos["horaEnvio"], PDO::PARAM_STR);
$stmt->bindParam(":telefono", $datos["telefono"], PDO::PARAM_INT);
if($stmt->execute()){
return "ok";
}else{
return "error";
}
$stmt->close();
$tmt =null;
}
}
And I add this other file called 'aux.php' in case it influences something in the error that causes me. Here is how to send 'purchases.controller.php' parameters within the 'ctrCash' function
if(isset($_POST['usuario'])){
require ('purchases.controller.php');
$arrayCompleto = json_decode($_POST['arrayCompleto'], true);
$usuario = $_POST['usuario'];
$direccion1 = $_POST['direccion1'];
$direccion2 = $_POST['direccion2'];
$dia = $_POST['dia'];
$hora = $_POST['hora'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$sesion = $_POST['sesion'];
$payments = new Purchases();
$payments -> ctrCash($arrayCompleto, $usuario, $direccion1, $direccion2, $dia, $hora, $email, $telefono, $sesion);
}
I have a php file called "purchases.controller.php" in which within a function called 'ctrCash' of the 'Purchases' class, I pass variables to a function called 'ctrNewCashPurchase' of the 'CartController' class that I have defined, but when I run the project, I get the message:
"Fatal error : Uncaught Error: Class 'CartModel' not found in ... "
If I do a var_dump inside the function ctrNewCashPurchase, I realize that I am entering that function, but it tells me that it does not recognize 'CartModel' and I do not understand why.
I share the code of the "purchases.controller.php" file:
class CartController{
static public function ctrNewCashPurchase($datos){
$tabla = "compras";
$respuesta = CartModel::mdlNewCashPurchase($tabla, $datos);
if($respuesta == "ok"){
$tabla = "comentarios";
ModeloUsuarios::mdlIngresoComentarios($tabla, $datos);
}
return $respuesta;
}
}
class Purchases {
public function ctrCash (&$arrayCompleto, &$usuario, &$direccion1, &$direccion2, &$dia, &$hora, &$email, &$telefono, &$sesion){
if(isset($usuario)){
//Here I create an array
for($i = 0; $i < count($arrayCompleto); $i++){
$datos = array("idUsuario"=> $sesion,
"idProducto"=> $arrayCompleto[$i]["idProducto"],
"metodo"=> "Efectivo",
"email"=> $email,
"direccion"=> $direccion1,
"detalleDireccion"=> $direccion2,
"diaEnvio"=> $dia,
"horaEnvio"=> $hora,
"telefono"=> $telefono,
"pais"=> "ARG");
}
$respuesta = CartController::ctrNewCashPurchase($datos);
}
}
}
I share the code of the "purchases.model.php" file, where I define the CartModel class:
class CartModel{
static public function mdlNewCashPurchase($tabla, $datos){
$stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (id_usuario, id_producto, metodo, email, direccion, pais, detalleDireccion, diaEnvio, horaEnvio, telefono) VALUES (:id_usuario, :id_producto, :metodo, :email, :direccion, :pais, :detalleDireccion, :diaEnvio, :horaEnvio, :telefono)");
$stmt->bindParam(":id_usuario", $datos["idUsuario"], PDO::PARAM_INT);
$stmt->bindParam(":id_producto", $datos["idProducto"], PDO::PARAM_INT);
$stmt->bindParam(":metodo", $datos["metodo"], PDO::PARAM_STR);
$stmt->bindParam(":email", $datos["email"], PDO::PARAM_STR);
$stmt->bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);
$stmt->bindParam(":pais", $datos["pais"], PDO::PARAM_STR);
$stmt->bindParam(":detalleDireccion", $datos["detalleDireccion"], PDO::PARAM_STR);
$stmt->bindParam(":diaEnvio", $datos["diaEnvio"], PDO::PARAM_STR);
$stmt->bindParam(":horaEnvio", $datos["horaEnvio"], PDO::PARAM_STR);
$stmt->bindParam(":telefono", $datos["telefono"], PDO::PARAM_INT);
if($stmt->execute()){
return "ok";
}else{
return "error";
}
$stmt->close();
$tmt =null;
}
}
And I add this other file called 'aux.php' in case it influences something in the error that causes me. Here is how to send 'purchases.controller.php' parameters within the 'ctrCash' function
if(isset($_POST['usuario'])){
require ('purchases.controller.php');
$arrayCompleto = json_decode($_POST['arrayCompleto'], true);
$usuario = $_POST['usuario'];
$direccion1 = $_POST['direccion1'];
$direccion2 = $_POST['direccion2'];
$dia = $_POST['dia'];
$hora = $_POST['hora'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$sesion = $_POST['sesion'];
$payments = new Purchases();
$payments -> ctrCash($arrayCompleto, $usuario, $direccion1, $direccion2, $dia, $hora, $email, $telefono, $sesion);
}
The error is beacause the model is not imported for the controller.
You can include it in the controller, it's the same (in this case).
if(isset($_POST['usuario'])){
require_once ('purchases.model.php');
require_once ('purchases.controller.php');
...
}
There is my code of EDIT.php DB_Functions,and g.php..I'm not geting where is the fault is anyone here who can help me to find out mistake on my code
Every things happen as easy but change in table is not reflecting..my SQL query is working properly on XAMP server..
It may be silly mistake but not able to find it..
edit.php
<?php
//error_reporting(0);
include("class_db.php");
include_once('DB_Functions.php');
if (isset ($_GET['edit_id']))
{
$id=$_GET['edit_id'];
{
if(isset($_POST['nam']))
{
$id =($_POST['edit_id']);
$name=($_POST['name']);
$lastname=($_POST['lastname']);
$email=($_POST['email']);
$duser=($_POST['duser']);
$pass=($_POST['pass']);
$mob=($_POST['mob']);
$website=($_POST['website']);
$result = file_get_contents('http://localhost/rajju/demo/webservises/webservises/webservices/g.php?action=update_details&id='.$id.'&name='.$name.'&lastname='.$lastname.'&email='.$email.'&duser='.$duser.'&pass='.$pass.'&mob='.$mob.'&website='.$website);
$result = json_decode($result, true);
if($result == 'success'){
header("location:http://localhost/rajju/demo/webservises/webservises/webservices/list.php");
}
else{
print_r($result);
}
}
}
}
$select =mysql_query("select * from users where id=$id");
$var = mysql_fetch_object($select);
?>
DB_Functions.php
public function updateUser($id,$name,$lastname,$email,$duser,$pass,$mob,$website)
{
$app_list =mysql_query("UPDATE users SET name='".$name."',lastname='".$lastname."',email='".$email."',duser='".$duser."',pass='".$pass."',mob='".$mob."',website='".$website."' WHERE id='".$id."'");
if ($app_list) {
return true;
} else {
return false;
}
}
g.php
else if($tag == 'update_details')
{
$db = new DB_Functions();
//$id = ($_GET['id']);
$name=($_GET['name']);
$lastname=($_GET['lastname']);
$email=($_GET['email']);
$duser=($_GET['duser']);
$pass=($_GET['pass']);
$mob=($_GET['mob']);
$website=($_GET['website']);
//exit (json_encode($name));
if ($db ->updateUser($name,$lastname,$email,$duser,$pass,$mob,$website))
{
exit (json_encode('success'));
}else
{
exit (json_encode('errorzz'));
}
}
The following should work. Note this still wont totally protect you against xss and other attacks. However its a lot better than using mysql_query!! Additionally, you should sanatise and check your incoming $_GET params and Salt+Hash your passwords.
<?php
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE users SET name=:name, lastname=:lastname, email=:email, duser=:duser, pass=:pass, mob=:mob, website=:website, WHERE id=:id";;
$st = $conn->prepare( $sql );
$st->bindValue(":name", $name, PDO::PARAM_STR);
$st->bindValue(":lastname", $lastname, PDO::PARAM_STR);
$st->bindValue(":email", $email, PDO::PARAM_STR);
$st->bindValue(":duser", $duser, PDO::PARAM_STR);
$st->bindValue(":pass", $pass, PDO::PARAM_STR);
$st->bindValue(":mob", $mob, PDO::PARAM_STR);
$st->bindValue(":website", $website, PDO::PARAM_STR);
$st->bindValue(":id", $id, PDO::PARAM_INT);
$st->execute();
?>
maybe this question is duplicate. but i cant find an answer for my problem.
i got this problem Fatal error: Call to undefined method mysqli_stmt::get_result() in demo.php on line 24
the line 24 is in this part of the code $tasks = $stmt->get_result();
here is my demo.php code.
<?php
/**
* Class to handle all db operations
* This class will have CRUD methods for database tables
*
* #author Ravi Tamada
* #link URL Tutorial link
*/
class Demo {
private $conn;
function __construct() {
require_once dirname(__FILE__) . '/include/db_connect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
public function getAllChatRooms() {
$stmt = $this->conn->prepare("SELECT * FROM chat_rooms");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getAllUsers() {
$stmt = $this->conn->prepare("SELECT * FROM users");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getDemoUser() {
$name = 'AndroidHive';
$email = 'admin#androidhive.info';
$stmt = $this->conn->prepare("SELECT user_id from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
if ($num_rows > 0) {
$stmt->bind_result($user_id);
$stmt->fetch();
return $user_id;
} else {
$stmt = $this->conn->prepare("INSERT INTO users(name, email) values(?, ?)");
$stmt->bind_param("ss", $name, $email);
$result = $stmt->execute();
$user_id = $stmt->insert_id;
$stmt->close();
return $user_id;
}
}
}
?>
in my index.php i have code to call the function.
this is the sample code
<?php
$chatrooms = $demo->getAllChatRooms();
foreach ($chatrooms as $key => $chatroom) {
$cls = $key == 0 ? 'selected' : '';
?>
<li id="<?= $chatroom['chat_room_id'] ?>" class="<?= $cls ?>">
<label><?= $chatroom['name'] ?></label>
<span>topic_<?= $chatroom['chat_room_id'] ?></span>
</li>
<?php
}
?>
please help me to construct this code.
thank you.
You need to install mysqlnd support. (MySQL Native Driver)
you can replace you function as bellow
i get problem like yours but this will solve please try it thank you
public function getAllChatRooms() {
$query = "SELECT chat_room_id, name, created_at FROM chat_rooms WHERE 1";
if ($result = mysqli_query($this->conn, $query)) {
return $result;
mysqli_free_result($result);
}
}
public function getAllUsers() {
$query = "SELECT user_id, name, email, gcm_registration_id, created_at FROM users WHERE 1";
if ($result = mysqli_query($this->conn, $query)) {
return $result;
mysqli_free_result($result);
}
}
When I run the PDO script alone it runs just fine But when I run it in a function I get this error Fatal error: Call to a member function prepare() on a non-object. I can not figure out why it is happening.
$pid = 6;
$custid = 1;
$sql = "SELECT COUNT(*) from signings WHERE pid = ? AND custid = ?";
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $pid, PDO::PARAM_INT);
$stmt->bindParam(2, $custid, PDO::PARAM_STR);
$stmt->execute();
$number_of_rows = $stmt->fetchColumn();
echo $number_of_rows;
$pid = 6;
$custid = 1;
function test($custid,$pid){
$sql = "SELECT COUNT(*) from signings WHERE pid = ? AND custid = ?";
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $pid, PDO::PARAM_INT);
$stmt->bindParam(2, $custid, PDO::PARAM_STR);
$stmt->execute();
$number_of_rows = $stmt->fetchColumn();
echo $number_of_rows;
}
echo test($custid,$pid);
The variable $db is not visible inside your function. you need to add
function test($custid,$pid){
global $db;
[...]
}
inside your function or you should pass the db object to your function via parameter:
function test($db, $custid,$pid){
[...]
}
another nice way ist to work with the factory patterns:
create a class db with a static method dbFactory and call it whenever you need to access your db
class db {
public static function dbFactory($host, $dbase, $user, $pass) {
$pdo = new PDO("mysql:host=$host;dbname=$dbase", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $pdo;
}
}
$db = db::dbFactory('localhost','mydbname','myusername','mypassword');