I am passing the number 1 from my javascript file as $played = json decode. I am not sure my Select query is working correctly, or my math is just all wrong. PHP is still new to me and I'm not sure if I can rename strings, and still have the query work. I know everything has to be passed into PHP to work and that I cant just make a variable and bind it and have the update work. I was thinking I could rename my passed JSON string so it could be used temporarily, but then I figured I could just name my SELECT string $results and then add the two.
The problem I am having is, it appears to UPDATE twice to the database. No matter how many times I wager, my database only shows the number 2. It's like it stops counting and UPDATE.
Any advice is welcome, thank you in advance for looking and helping if you can.
Okay this didn't work. Maybe I'm not understanding the answer. It didn't post anything to the database. Do I still need to bind the values, or no?
<?php
session_start();
if(empty($_SESSION['userid']))
{
header("Location: ../login/index.php");
}
include('../../login/database.php');
if (isset($_SESSION['userid']))
$played = json_decode(file_get_contents('php://input'), true);
$userid = $_SESSION['userid'];
try {
$db = DB();
$stmt = $db->prepare("UPDATE usersystem SET played = played + :played WHERE userid = :userid");
$stmt->bindValue(':played', $played, PDO::PARAM_STR);
$stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
$db = null;
echo $e->getMessage();
}
}
echo $_POST['package'];
?>
And here is the javascript POST
function updateDatabase() {
played = 1;
var package = played;
//console.log(package);
fetch('../php/played/played.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(package)
});
}
This is getting aggravating. I have this Version information: 4.6.4 , latest stable version: 4.9.7 phpmyadmin.
Since my played.php will not post the data to the database I decided to use my credit.php file to test the played entry I am trying to make. Here is how I tested it, and this too does not post the info to my database, but my credits do get posted into the database.
credits.php
<?php
session_start();
if(empty($_SESSION['userid']))
{
header("Location: ../login/index.php");
}
include('../../login/database.php');
if (isset($_SESSION['userid'])) {
$money = json_decode(file_get_contents('php://input'), true);
$money_as_number = intval( $money );
$userid = $_SESSION['userid'];
try {
$db = DB();
//$a = 1;
//$db->query("update usersystem set played=played+". intval($a) ."WHERE userid=:userid");
$db->query("UPDATE usersystem set played = played + 1 WHERE userid=:userid");
$stmt = $db->prepare("UPDATE usersystem SET money=:money WHERE userid=:userid");
$stmt->bindValue(':money', $money_as_number, PDO::PARAM_STR);
$stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
$db = null;
echo $e->getMessage();
}
}
echo $_POST['package'];
?>
Here is my money Ajax which is identical to the played ajax
var package = money;
//console.log(package);
fetch('../php/credits/credit.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(package)
});
Maybe I can't do played = played + with my version? Any ideas?
I solved it finally !!! Thank you very much for answering and educating. Your answer led me to the research i needed so I could understand things better and let me tell you, no more query for me, and have a way better understanding of fetch and how it's used!
<?php
session_start();
if(empty($_SESSION['userid']))
{
header("Location: ../login/index.php");
}
include('../../login/database.php');
if (isset($_SESSION['userid'])) {
$money = json_decode(file_get_contents('php://input'), true);
$money_as_number = intval( $money );
$userid = $_SESSION['userid'];
try {
$db = DB();
$stmt = $db->prepare("SELECT played FROM usersystem WHERE userid=?");
$stmt->execute([$userid]);
$played = $stmt->fetchColumn();
$results = 1;
$played = $results + $played;
$stmt = $db->prepare("UPDATE usersystem SET money=:money, played=:played WHERE userid=:userid");
$stmt->bindValue(':money', $money_as_number, PDO::PARAM_INT);
$stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
$stmt->bindValue(':played', $played, PDO::PARAM_INT);
$stmt->execute();
}
catch(PDOException $e)
{
$db = null;
echo $e->getMessage();
}
}
//echo $_POST['package'];
?>