mysql - PHP _GET issue not refreshing data on multiple tries
I have a php script that is triggered from my app. This script uses the GET method to retrieve information from the url and put it into a database. I have got it to work so far except for the fact when I try to do multiple requests.
For example, I did a test where every time I would take the name of the database from the GET, put it into the appropriate database, retrieve the last entry, and then it would add 7 each time and echo it to the screen/put it back into the database as the newest number. I tried this within my browser hitting refresh every time and the first time it worked. Second time I hit refresh, it gave me the same results as the first time. I kept trying but same result so I closed the browser and tried it again. It worked as it should.
So the problem I think has to do with the script not receiving the connection the second, third, fourth... time around. It seems to only work the first time. I thought this was an issue with the data being cached but I turned off cache within the php script.
Can someone please tell me why it only works on the first attempt?
Thanks
EDITED WITH CODE
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
checkToken();
function checkToken ()
{
$username='XXXXX';
$password='XXXXX';
$database='Push';
$passed= $_GET['token'];
$pieces = explode(" ", $passed); /*Contains two pieces of information.
First is for the correct table and
and the second is the actually data
that will be put into the tables*/
$search= $pieces[1];
$table= $pieces[0];
echo $table." ".$search;
$db= new mysqli('localhost', $username, $password, $database);
if (mysqli_connect_errno())
{
echo 'Error: Could not connect';
exit;
}
$query = "SELECT * FROM `Tokens` WHERE `Number` LIKE '%$search%' ";
echo "after";
$result = $db->query($query);
if ($result) {
$num_results = $result->num_rows;
$row = $result->fetch_assoc();
echo $row;
if (!$row)
{
echo "No Token, insert into database";
insertToken($table,$pieces[1]);
return;
}
/*problem may be around here...? does not always go into (!row)
after the first time when it is supposed too*/
echo $row['Numbers'];
} else {
// echo "Query failed: {$db->error}\n";
echo "No";
}
}
function insertToken ($daTable, $daToken)
{
$username='XXXX';
$password='XXXX';
$database='Push';
$token=$daToken;
$con= mysql_connect("localhost",$username,$password);
mysql_select_db($database,$con);
$res = mysql_query(
"INSERT INTO $daTable
VALUES ('', '$token')");
if (!$res) {
die('Invalid query: ' . mysql_error());
} else {
echo "complete";
}
mysql_close($link);
}
Answer
Solution:
maybe a cache problem? try to make sure the code gets executed at all, like echoing the time