从<a href=""> link uncaught referenceError</a>更新Mysql值

时间:2014-06-27 07:23:09

标签: php mysql

我是php和mysql的新手,并且在理解为什么更新数据库中的值的特定函数无法正常工作时遇到问题。

我希望能够在用户点击链接时将“0”int更改为“1”。 (我使用值0/1来跟踪用户帐户是否处于活动状态。)

相关链接显示:

<?php

    $user = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);

    //check to see if the user is active or not
    if ($user [0]['active'] == 0)
    {
    printf("Your account is not currently active ");
    printf('<a href="#" onclick="activate();">Click Me</a>');
    printf(" to reactivate");
    }
    //assuming they have logged in they will probably want to make themselves active

?>

当我点击控制台报告的链接时:

Uncaught ReferenceError: activate is not defined

我已在一个单独的functions.php文件中定义了activate,该文件正在加载并具有正确的权限。 (我确信它正在被上面的代码加载,因为当我在上面的代码中手动定义激活时,我得到一个错误,告诉我我无法定义具有相同名称的2个函数)。

functions.php部分显示:

function activate()
    {
    require("../templates/activate_user.php");
    exit;
    }

最后,activate_user.php读取:

<?php
    // configuration
        require("../includes/config.php"); 

    query("UPDATE users SET active = 1 WHERE id = ?", $_SESSION["id"]);
    return false;

?>

我搜索并搜索了如何修复此错误,但我无法解决此问题。我猜它可能与激活范围有关,但我不确定这是正确的路径。

任何得到很好的帮助,这是我第一次进入php / mysql,所以欢迎所有的观点。

感谢;

安迪

2 个答案:

答案 0 :(得分:0)

onclick =“activate();”是Javascript代码。其余代码是用PHP编写的。两者都不会以这种方式一起工作。

答案 1 :(得分:0)

为了保持简单,我通过调用同一页面并在该页面中定义函数来解决问题:

<?php

function activate()
    {
    require("../templates/activate_user.php");
    exit;
    }

//check to see if the user is active or not
    if ($user [0]['active'] == 0)
    {
    printf("Your account is not currently active ");
    printf('<a href="index.php?action=callfunction">Click Me</a>');
    printf(" to reactivate");

    //assuming they have logged in they will probably want to make themselves active
    if(isset($_GET['action'])&& $_GET['action'] =='callfunction'){
    activate();
    }

    }
?>

不确定它是否漂亮,但确实有效。感谢您的帮助和评论(使搜索更容易!)