title: Doing php select to use the string within math problem with Json Decoded string and then update my database
body: <p>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.</p>
  <p>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.</p>
  <p>Any advice is welcome, thank you in advance for looking and helping if you can.</p>
  <p>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?</p>
  <pre><code>   &lt;?php 
  session_start();
  if(empty($_SESSION['userid']))
  {
      header(&quot;Location: ../login/index.php&quot;);
  }
  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-&gt;prepare(&quot;UPDATE usersystem SET played = played + :played WHERE userid = :userid&quot;);
  $stmt-&gt;bindValue(':played', $played, PDO::PARAM_STR);
  $stmt-&gt;bindValue(':userid', $userid, PDO::PARAM_STR);
  $stmt-&gt;execute();
  
  }
  catch(PDOException $e)
  {
      $db = null;  
      echo $e-&gt;getMessage();  
  }
  }
  
  echo $_POST['package']; 
  ?&gt;
  </code></pre>
  <p>And here is the javascript POST</p>
  <pre><code>    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)
          });
  }
  </code></pre>
  <p>This is getting aggravating. I have this Version information: 4.6.4 , latest stable version: 4.9.7 phpmyadmin.</p>
  <p>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.</p>
  <p>credits.php</p>
  <pre><code>    &lt;?php 
  session_start();
  if(empty($_SESSION['userid']))
  {
      header(&quot;Location: ../login/index.php&quot;);
  }
  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-&gt;query(&quot;update usersystem set played=played+&quot;. intval($a) .&quot;WHERE userid=:userid&quot;);
  $db-&gt;query(&quot;UPDATE usersystem set played = played + 1 WHERE userid=:userid&quot;);
  
  $stmt = $db-&gt;prepare(&quot;UPDATE usersystem SET money=:money WHERE userid=:userid&quot;);
  $stmt-&gt;bindValue(':money', $money_as_number, PDO::PARAM_STR);
  $stmt-&gt;bindValue(':userid', $userid, PDO::PARAM_STR);
  $stmt-&gt;execute();
  
  }
  catch(PDOException $e)
  {
      $db = null;  
      echo $e-&gt;getMessage();  
  }
  }
  
  echo $_POST['package']; 
  ?&gt;
  </code></pre>
  <p>Here is my money Ajax which is identical to the played ajax</p>
  <pre><code>    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)
  });
  </code></pre>
  <p>Maybe I can't do played = played + with my version? Any ideas?</p>
  <hr />
  <p>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!</p>
  <pre><code>    &lt;?php 
  session_start();
  if(empty($_SESSION['userid']))
  {
      header(&quot;Location: ../login/index.php&quot;);
  }
  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-&gt;prepare(&quot;SELECT played FROM usersystem WHERE userid=?&quot;);
  $stmt-&gt;execute([$userid]);
  $played = $stmt-&gt;fetchColumn();
  
  $results = 1;
  $played = $results + $played;
  
  
  $stmt = $db-&gt;prepare(&quot;UPDATE usersystem SET money=:money, played=:played WHERE userid=:userid&quot;);
  $stmt-&gt;bindValue(':money', $money_as_number, PDO::PARAM_INT);
  $stmt-&gt;bindValue(':userid', $userid, PDO::PARAM_STR);
  $stmt-&gt;bindValue(':played', $played, PDO::PARAM_INT);
  $stmt-&gt;execute();
  
  }
  catch(PDOException $e)
  {
      $db = null;  
      echo $e-&gt;getMessage();  
  }
  }
  
  //echo $_POST['package']; 
  ?&gt;
  </code></pre>