0

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.

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Hifi
  • 7
  • 6

2 Answers2

1

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."));
 }
}
Nishant Nair
  • 1,999
  • 1
  • 13
  • 18
  • Its showing the url where i wanted but generating new key each time i hit the generate what i need is it don't generate new link but show the old generated link against same domain and article. the url is : domain/key – Hifi Jan 12 '17 at 12:41
0

$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."));
radicarl
  • 327
  • 2
  • 9
  • After implementing your code i am getting error, i am sure i did not put the syntax right. this is how it look like now (unable to post complete code in the comment, here is the screenshot) : http://prntscr.com/duoxol Please let me know what i am doing wrong. – Hifi Jan 12 '17 at 13:24
  • the error message could be helpful and the line of code it affects. As I wrote in the code comment, i do not know, what your getRow-function returns. It could have been an array, but maybe it is not and this is the reason for your error. try `var_dump($result)` instead of `$key = $result['key'];` or post the code of getRow – radicarl Jan 12 '17 at 13:33
  • Actually it do nothing, when i hit generate button it don't do anything or show any output or error, this mostly have happen when the code have any syntax error, i can be wrong. did you check the screenshot if its how you wanted ? – Hifi Jan 12 '17 at 13:38
  • Same with var_dump($result). Code of getRow ? it don't have the code, it Runs the query provided and puts the first row of data into an array then frees the result set. – Hifi Jan 12 '17 at 13:46
  • add a `error_reporting(E_ALL);` at the beginning of your file and post the your complete code by editing your question. The getRow function is not a default php function, so it must be somewhere in your appliction. – radicarl Jan 12 '17 at 13:49
  • * Helper function to get single row right out of query and optional arguments `public function getRow() { $query = $this->prepareQuery(func_get_args()); if ($res = $this->rawQuery($query)) { $ret = $this->fetch($res); $this->free($res); return $this->ret($ret); } return FALSE; }` found it. – Hifi Jan 12 '17 at 14:12
  • found the mistake: `$db->getRow` – radicarl Jan 12 '17 at 14:22
  • Still same :( and there is nothing with error reporting. I really very appreciate all the effort and time yo put into it. – Hifi Jan 12 '17 at 14:36
  • we need the error. Try to fix your error reporting. Maybe this is helpful: http://stackoverflow.com/questions/1475297/phps-white-screen-of-death – radicarl Jan 12 '17 at 14:45