显示针对相同输入的旧生成密钥

时间:2017-01-12 12:11:59

标签: php sql

这是我正在处理的代码

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."));
    }
}

它为用户生成url然后显示它,如果已经为同一篇文章生成了url,那么这就是最后一个" else"来

我想要的是,即使之前生成了网址,它仍会显示旧的生成网址并重新分发文字

我尝试$domain.$key." Resharing."显示了域名,但没有显示密钥

我也试过

echo json_encode(array("status" => "Success", "url" => $http.$tracking_url));

没有运气

我不知道我有多好解释我的问题,如果我需要解释更多,请告诉我。

2 个答案:

答案 0 :(得分:1)

请查找更新的代码 这是因为一旦条件被跳转到if循环就生成了密钥,否则没有找到密钥。

 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."));
 }
}

答案 1 :(得分:0)

$ key未在您的上一个else分支中设置。我假设函数getRow返回db的结果,这个结果包含密钥。

$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."));