首页 > 解决方案 > php函数返回某些页面的值,但无法返回具有相同文档结构的其他页面的值

问题描述

因此,在这里我想使用 php 页面获取特定用户的 ID,然后使用在名为“functions.php”的文件中定义的函数执行数据库插入。所以基本上,我使用 jQuery ajax() 方法从页面捕获值并将数据发送到名为“follow_school.php”的 php 页面。从代码中可以清楚地看到其余的情况,我在下面提供:

jQuery代码

jQuery(document).ready(function() {
    var school_name = jQuery('#school-name').text();
    var session_var = jQuery('#session').text();
    // console.log(session_var);
    var button_text_onload = 
    localStorage.getItem("btnText_"+school_name+"_for_"+session_var);
    console.log(button_text_onload);
    if (button_text_onload !== null) {
        jQuery('#follow-button').html(button_text_onload);
    } else {
        jQuery('#follow-button').html('Follow');
    }
    jQuery('#follow-button').click(function() {
        // alert (session_var);
        // console.log(session_var);
        var button_text = jQuery('#follow-button').text();
        // alert(button_text);
        if (button_text === 'Follow') { 
            jQuery.ajax({
                type: 'POST',
                url: 
         'https://mim-insider.com/wp-content/themes/Divi/follow_school.php',
                data: {name : school_name,
                      email : session_var
                },
                success: function(result) {
                    console.log(result);
                    var key = "btnText_" + school_name +"_for_" + 
                               session_var;
                    console.log(key);
                    localStorage.setItem(key, "Unfollow");
                jQuery('#follow-button').html('Unfollow');
            }});
        } else {
            jQuery.ajax({
                type: 'POST',
                url:
       'https://mim-insider.com/wp-content/themes/Divi/unfollow_school.php',
                data: {name : school_name,
                       email : session_var,
                },
                success: function(result) {
                    console.log(result);
                    var key = "btnText_" + school_name +"_for_" + 
                               session_var;
                    console.log(key);
                    localStorage.setItem(key, "Follow");
                jQuery('#follow-button').html("Follow");
            }});
        }
    });
});

follow_school.php

<?php   
    include_once "includes/db.php";
    include_once "includes/functions.php";

    $name = $_POST['name'];
    $email = $_POST['email'];
    $id = get_id($email);

    /* for debugging purpose */
    if ($id == null) {
        var_dump($id);
    } else {
        echo $id;
    }
    echo $email;

    /* insert operation */
    insert_school($id, $name);
?>

函数.php

function get_id($email) {
    global $conn;

    $sql = "SELECT id FROM member WHERE email = '$email'";

    $result = $conn->query($sql);

    $row = $result->fetch_assoc();

    return $row["id"];
}

function insert_school($id, $name) {
    global $conn;

    $sql = "INSERT INTO user_schools (user_id, school_name)
    VALUES ('$id', '$name')";

    $conn->query($sql);
}

注意:这里的数据库连接非常好,并且这些代码之前适用于某些页面。但显然,其他页面似乎没有运行代码,或者准确地说,返回“null”作为 id。另外,我想提一下,表条目和字段不是空的,也不是未指定的。正如我所说,它们非常好,它适用于某些页面。

请任何人指导我,这里出了什么问题,因为在我花了这么多时间在这上面之后,我仍然无法理解这个问题。谢谢你。

标签: phpjquery

解决方案


推荐阅读