This is the code i am working on
if(isset($_POST['generate'])) {
$article_id = $_POST['generate']['id']; //escape string
$domain = $_POST['generate']['domain']; //escape string
$userid = $_POST['generate']['uid']; //escape string
if(!$db->getRow("SELECT * FROM `".PREFIX."user_stats` WHERE `article_id` = ?i AND `domain` = ?s AND `userid` = ?i", $article_id, $domain, $userid)) {
$key = randomIDGenerator();
$http = "http://";
$tracking = array(
"userid" => $userid,
"username" => $user->data->username,
"article_id" => $article_id,
"key" => $key,
"domain" => $domain,
"created" => $time
);
if(($db->query("INSERT INTO `".PREFIX."user_stats` SET ?u", $tracking)) && ($id = $db->insertId()) && ($db->query("UPDATE `".PREFIX."articles` SET `shared` = `shared` + ?i WHERE `article_id` = ?i", 1, $article_id))) {
$tracking_url = $domain.$key;
echo json_encode(array("status" => "Success", "url" => $http.$tracking_url));
} else {
echo json_encode(array("status" => "Error", "msj" => "Internal error. Please refresh the page and try again."));
}
} else {
echo json_encode(array("status" => "Error", "msj" => $domain.$key." Resharing."));
}
}
It generate url for user and then show it, and if the url is already generated for same article then this is where the last "else" come
what I want is if even the url is generated before, it still show old generated url with resharing text
I tried $domain.$key." Resharing."
which shows the domain but not the key
and also I tried
echo json_encode(array("status" => "Success", "url" => $http.$tracking_url));
with no luck
I don't know how good i explained my problem please let me know if i need to explain more.
$key is not set in your last else-branch. I assume the function getRow returns a result from the db and this result contains amongst others the key.
$result = $db->getRow("SELECT * FROM `".PREF."...");
if(!$result) {
$key = randomIDGenerator();
$http = "http://";
$tracking = array(
//...
} else {
// a guess how you could access the key from your result
// depends on the return value of the function getRow
$key = $result['key'];
echo json_encode(array("status" => "Error", "msj" => $domain.$key." Resharing."));
Please find the updated code It was due to key was generated in if loop once the condition was jumped to else no key was found.
if(isset($_POST['generate'])) {
$article_id = $_POST['generate']['id']; //escape string
$domain = $_POST['generate']['domain']; //escape string
$userid = $_POST['generate']['uid']; //escape string
$key = randomIDGenerator();
if(!$db->getRow("SELECT * FROM `".PREFIX."user_stats` WHERE `article_id` = ?i AND `domain` = ?s AND `userid` = ?i", $article_id, $domain, $userid)) {
$http = "http://";
$tracking = array(
"userid" => $userid,
"username" => $user->data->username,
"article_id" => $article_id,
"key" => $key,
"domain" => $domain,
"created" => $time
);
if(($db->query("INSERT INTO `".PREFIX."user_stats` SET ?u", $tracking)) && ($id = $db->insertId()) && ($db->query("UPDATE `".PREFIX."articles` SET `shared` = `shared` + ?i WHERE `article_id` = ?i", 1, $article_id))) {
$tracking_url = $domain.$key;
echo json_encode(array("status" => "Success", "url" => $http.$tracking_url));
} else {
echo json_encode(array("status" => "Error", "msj" => "Internal error. Please refresh the page and try again."));
}
} else {
echo json_encode(array("status" => "Error", "msj" => $domain.$key." Resharing."));
}
}