Why would it not work to call the get_accounts() function at the end of the delete_account() function?
function get_accounts() {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->query("SELECT * FROM account");
} catch (Exception $e) {
echo ("ERROR: Data could not be retrieved from the database." . $e);
exit;
}
$accounts = $results->fetchall(PDO::FETCH_ASSOC);
return $accounts;
}
if(isset($_GET['action']) && ($_GET['action'] == 'delete_account')) {
require("config.php");
require("database.php");
$deleteAccount = $_POST['account'];
try {
$results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
$results->bindValue(1, $deleteAccount);
$results->execute();
} catch(Exception $e) {
echo "ERROR: Data could not be removed from the database. " . $e;
exit;
}
echo($deleteAccount);
get_accounts();
};
Basically, I want to run the delete_accounts() function and at the end I would like to run the get_accounts() function, which will refresh the list of accounts on the page after the selected account has been deleted. I can't seem to call a function from within another function, no matter what I try.
Use the finally part of the try catch & remove the 'exit();'
if(isset($_GET['action']) && ($_GET['action'] == 'delete_account')) {
require("config.php");
require("database.php");
$deleteAccount = $_POST['account'];
try {
$results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
$results->bindValue(1, $deleteAccount);
$results->execute();
} catch(Exception $e) {
echo "ERROR: Data could not be removed from the database. " . $e;
}finally{
get_accounts();
}
echo($deleteAccount);
}
Related
Hi I get the "There is no active transaction" when I run my code below:
public function Login($username, $password)
{
try
{
$database = db_camagru();
$query = "SELECT id FROM users WHERE (username=:username OR email=:username) AND password=:password";
$database->exec($query);
$database->commit();
if ($query->rowCount() > 0)
{
$result = $query->fetch(PDO::FETCH_OBJ);
return $result->id;
}
else
{
return false;
}
}
catch (PDOException $e)
{
exit($e->getMessage() . "kwezi");
}
}
the error seems to be coming from my $database->commit(); line.
Hei,
I need a function in php that checks if a value entered by form is already in database (sql - server -- PDO), and return TRUE or FALSE.
I tried to do this, but I got stuck and didn't found a solution on internet.
could you give me a hint on how to threat the above condition ?
function check_code($code) {
GLOBAL $handler;
$code = check_input($code);
try{
$query2 = $handler -> prepare("SELECT code from stock where code = :code");
$query2 -> execute(array(
':code' => $code
));
return >???<
}
catch(PDOException $e){
echo $e -> getMessage();
} }
return something like row_count(result) > 0
I've never work with sql-server before but I had worked with PDO many times, basically this is how would check in pdo
<?php
function check_code($code)
{
GLOBAL $handler;
$code = check_input($code);
try {
$query2 = $handler->prepare("SELECT code from stock where code = :code");
$query2->execute(array(':code' => $code));
$results = $query2->fetchColumn();
if (count($results) > 0) {
echo "exists";
} else {
echo "does not exist";
}
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
?>
NB: Avoid using the Global var... Stop using `global` in PHP
Hi guys I have a problem with getting a variable outside and after an if statement. Little code explanation: $gameid or $id is sent via ajax to this file. Now $id have always a value and if (isset($_GET['id'])) { is true. The insert and select querys work very well. The problem is that I cant get the variable $answer or $question[0]['question']; after the second if statement. I can get them when I change the second if (isset($_GET['id'])) { to else { but then anyhow there become two rows in my database inserted, the first is right and the second one is empty. Now why cant I get the variables $answer and $question[0]['question']; after the second if condition?
The error log shows: Notice undefined variable question.
<?php
$hostname='localhost';
$user='';
$password='';
if (isset($_GET['gameid'])) {
$gameid = $_GET['gameid'];
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
echo $id;
}
if (isset($_GET['questionid'])) {
$questionid = $_GET['questionid'];
}
$new = 0;
if (isset($_GET['gameid'])) {
try {
$dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT *
FROM questions_de
WHERE id = '$questionid'
LIMIT 1";
if ($res = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$question = $res->fetchAll();
}
if($question > 0) {
//do something
} else {
echo "Sorry something happen wrong with our servers.";
}
}
catch(PDOException $e) {
}
if ($question[0]["answerm²"] == 0 && $question[0]["answerm³"] == 0) {
$answer = "answer_m";
} else {
$answer = "answerm³";
}
}
if (isset($_GET['id'])) {
try {
$dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT username
FROM user
WHERE id = '$id'
LIMIT 1";
if ($res = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$user2name = $res->fetchAll();
}
if($user2name > 0) {
//do something
} else {
echo "Sorry something happen wrong with our servers.";
}
}
catch(PDOException $e) {
}
try {
$dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT *
FROM questions_de
LIMIT 1"; //
if ($res = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$question = $res->fetchAll();
}
if($question > 0) {
//do something
} else {
echo "Sorry something happen wrong with our servers.";
}
}
catch(PDOException $e) {
}
if ($question[0]["answerm²"] == 0 && $question[0]["answerm³"] == 0) {
try {
$dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO game_create (user1, user2, user1name, user2name, question, questionid, answer)
VALUES ('".$_COOKIE["userid"]."', '".$id."', '".$_COOKIE["username"]."', '".$user2name[0]["username"]."', '".$question[0]['question']."', '".$question[0]['id']."', '".$question[0]['answer_m']."')";
if ($dbh->query($sql)) {
//echo "New Record Inserted Successfully";
} else{
// echo "Data not successfully Inserted.";
}
$new = $dbh->lastInsertId();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
if ($new > 0) {
} else {
echo 'Sorry something went wrong.';
}
$answer = "answer_m";
}
else {
try {
$dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO game_create (user1, user2, user1name, user2name, question, questionid, answer)
VALUES ('".$_COOKIE["userid"]."', '".$id."', '".$_COOKIE["username"]."', '".$user2name[0]["username"]."', '".$question[0]['question']."', '".$question[0]['id']."', '".$question[0]['answer_m³']."')";
if ($dbh->query($sql)) {
//echo "New Record Inserted Successfully";
} else{
// echo "Data not successfully Inserted.";
}
$new = $dbh->lastInsertId();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
if ($new > 0) {
} else {
echo 'Sorry something went wrong.';
}
$answer = "answerm³";
}
}
?>
On the line:
if (isset($_GET['gameid'])) {..}
You're checking if the $_GET['gameid'] is set, with other words, if it has a value. As you stated, $_GET['gameid'] only has a value when $_GET_[id] doesn't have value. As you said, $_GET_['id] always has value, so $_GET['gameid'] wont be initialized.
Therefor, it won't get past this condition, and won't reach the line of $question = $res->fetchAll();, which initializes the variable.
The same thing stands for $answer, since it's in the same if statement and is also doing conditional checking on the $question variable.
To solve this problem, either initialize the $_GET['gameid'] variable by sending it to the PHP script without any conditions or remove the if (isset($_GET['gameid'])) {..} statement.
I have integrated google loing to my website. It's working fantastic. When someone logs in via google for the firs time, then a new entry is stored in the database.
But, when he logs in again..only the last login (a column on the table) should be updated...but instead, mysql adds a new row.
What am I doing wrong here?
public function trigger_registration_from_google($fname,$lname,$email)
{
global $conn;
try
{
if(useremailexists($email))
{
$date = date('Y-m-d');
//run update query
//user already exists, only update
try
{
$s = $conn->prepare("UPDATE users set last_login = :last_login where emailid = :email ");
$s->bindParam(':last_login',$date);
$s->bindParam(':email',$email);
$s->execute();
$s->closeCursor();
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{
//insert
//insert now..since he is a new user
$date = date('Y-m-d');
$v=1;
$r="google";
try
{
$s = $conn->prepare("INSERT INTO users(fname,lname,emailid,registeredby,registeredon,last_login,verified) values (:fname,:lname,:emailid,:registeredby,:registeredon,:last_login,:verified)");
$s->bindParam(':fname',$fname);
$s->bindParam(':lname',$lname);
$s->bindParam(':emailid',$email);
$s->bindParam(':registeredby',$r);
$s->bindParam(':registeredon',$date);
$s->bindParam(':last_login',$date);
$s->bindParam(':verified',$v);
$s->execute();
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}//function
Edit
useremailexists
function useremailexists($email)
{
//check if the email exists
global $conn;
try
{
$s = $conn->prepare("SELECT * from users where emailid = :email");
$s->bindParam(':email',$email);
$s->execute();
if($s->rowCount() > 0)
{
return true;
}
else
{
return false;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}//function
Validate if the function useremailexist return true or false , we can't help you without this piece of code.
Today i tired pass from the mysql connection to PDO. And i met a problem.
require('config.php');
function GetAll($query, $params) {
global $db;
try {
$sth = $db->prepare($query);
}
catch (PDOException $e) {
return null;
}
try {
$sth->execute($params);
}
catch (PDOException $e) {
return null;
}
$result = $sth->fetchAll();
return $result;
}
if ($fetch = GetAll("SELECT `loggedip` FROM `ipcheck` WHERE `loggedip`=':ipcheck'", array(":ipcheck" => $iptocheck))) {
$resultx = $db->prepare("SELECT `failedattempts` FROM `ipcheck` WHERE `loggedip`='$iptocheck'");
$resultx->execute();
while ($rowx = $resultx->fetch()) {
;
}
$loginattempts_total = $rowx['failedattempts'];
echo "$loginattempts_total";
if ($loginattempts_total > $maxfailedattempt) {
header(sprintf("Location: %s", $forbidden_url));
exit;
}
}
this is my script. in PDO and his don't work. when my ip is banned should not see, but i see the page. PLEASE HELP ((