HTML表单不通过PHP文件更新mySQL

时间:2015-07-08 13:07:48

标签: php html mysql

我不太清楚发生了什么,因为我在网页上遵循与网站上其他几个页面相同的实施概念。出于某种原因,此页面不会更新数据库。它不会抛出任何错误或任何内容,只是不更新​​用户信息。

HTML在顶部显示文本框和单选按钮,以便管理员用户可以键入名称并选择是否提升,降级,激活或停用在文本框中输入的帐户。提交后,代码不会对我大喊大叫,而是什么都不做。我尝试了几种不同的方法,但无论它是什么,都没有更新数据库。

这是HTML文件(缩写):

  <?php include_once ($_SERVER['DOCUMENT_ROOT'] . '/public_html/templates/areadmin.php');


    if(isset($_POST['user']))
    {
        checkfunc();
    }
    else
    {   
?>

<!-- Contents of the page-->
<div class="container2">
    <div align="center"><br/><br/>
        <div id="box1"><br/><br/><br/>
            <form action="?checkfunc" style="display:inline; margin:2px;" method="post">
                <input type="text" autocomplete="off" size="15" placeholder="Username" name="user"><br/>
                <input type="radio" name="act" value="promote">Promote
                <input type="radio" name="act" value="demote">Demote
                <input type="radio" name="act" value="activate">Activate
                <input type="radio" name="act" value="deactivate">Deactivate
                <br/><input type="submit" value="Submit">
            </form>


                <?php include_once ($_SERVER['DOCUMENT_ROOT'] . '/public_html/PHP/action.php'); listmembers($db_handle); ?>
            </table><br/><br/><br/><br/>
        </div>
    </div>
</div>

<?php
    }
    function checkfunc()
    {
        $selected = $_REQUEST['act'];

        if($selected == "promote")
        {
            promote($db_handle);
        }
        elseif($selected == "demote")
        {
            demote($db_handle);
        }
        elseif($selected == "activate")
        {
            activate($db_handle);
        }
        elseif($selected == "deactivate")
        {
            deactivate($db_handle);
        }
    }
?>

PHP文件中的函数:

$db_handle = mysqli_connect($server, $user_name, $pass_word, $database);

//Date: June 25, 2015
//Description: promotes a user to admin
function promote($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];

        //Updates user to admin
        mysqli_query($db_handle, "UPDATE `user` SET isAdmin = 1 WHERE username = '$user'");
        header ("Location: /public_html/HTML/memberlist.html");
}

//Date: June 25, 2015
//Description: demotes a user to standard
function demote($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];

        if($user != $_SESSION['username'] && $user != "admin")
        {
            //Updates user to admin
            mysqli_query($db_handle, "UPDATE `user` SET isAdmin = 0 WHERE username = '$user'");
            header ("Location: /public_html/HTML/memberlist.html");
        }
        else
        {
            echo "nope.";
        }
}

//Date: June 25, 2015
//Description: activates an inactive account
function activate($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];

        //Updates user to admin
        mysqli_query($db_handle, "UPDATE `user` SET isActive = 1 WHERE username = '$user'");
        header ("Location: /public_html/HTML/memberlist.html");
}

function deactivate($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];

        //Updates user to admin
        mysqli_query($db_handle, "UPDATE `user` SET isActive = 0 WHERE username = '$user'");
        header ("Location: /public_html/HTML/memberlist.html");
}

有谁能告诉我数据库为什么不接受我的更新语句?

更新

通过错误处理,它告诉我$ .db_handle是未定义的,并且在.php函数中是null,但是,.php中有十几个函数,并且所有函数都有$ db_handle作为参数和工作。甚至listmembers($ db_handle)也用在同一个.html上,并没有给出错误。

1 个答案:

答案 0 :(得分:0)

I solved the issue by creating a new function in the .php file to check the function that needs to be run and passing it the global variable $db_handle.

HTML:

<?php include_once ($_SERVER['DOCUMENT_ROOT'] . '/public_html/templates/areadmin.php');

if(isset($_POST['user']))
{
    checkfunc();
}
else
{   
?>

<!-- Contents of the page-->
<div class="container2"> 
<div align="center"><br/><br/>
    <div id="box1"><br/><br/><br/>
        <form action="?checkfunc" style="display:inline; margin:2px;" method="post">
            <input type="text" autocomplete="off" size="15" placeholder="Username" name="user"><br/>
            <input type="radio" name="act" value="promote">Promote
            <input type="radio" name="act" value="demote">Demote
            <input type="radio" name="act" value="activate">Activate
            <input type="radio" name="act" value="deactivate">Deactivate
            <br/><input type="submit" value="Submit">
        </form>
    </div>
</div>
</div>
<?php
}
function checkfunc()
{
    include_once ($_SERVER['DOCUMENT_ROOT'] . '/public_html/PHP/action.php');
    checkfunction($db_handle);
}
?>

PHP:

//Author: Lance Rainey
//Date: June 25, 2015
//Description: promotes a user to admin
function checkfunction()
{
        global $db_handle;

        $selected = $_REQUEST['act'];

        if($selected == "promote")
        {
            promote($db_handle);
        }
        elseif($selected == "demote")
        {
            demote($db_handle);
        }
        elseif($selected == "activate")
        {
            activate($db_handle);
        }
        elseif($selected == "deactivate")
        {
            deactivate($db_handle);
        }
}

    //Author: Lance Rainey
//Date: June 25, 2015
//Description: promotes a user to admin
function promote($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];
        $query = "UPDATE `user` SET isAdmin = 1 WHERE username = '$user'";
        //Updates user to admin
        mysqli_query($db_handle, $query) or die(mysqli_error($db_handle));
        header ("Location: /public_html/HTML/memberlist.html");
}

    //Author: Lance Rainey
//Date: June 25, 2015
//Description: demotes a user to standard
function demote($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];
        $query = "UPDATE `user` SET isAdmin = 0 WHERE username = '$user'";

        if($user != $_SESSION['username'] && $user != "admin")
        {
            //Updates user to admin
            mysqli_query($db_handle, $query) or die(mysqli_error($db_handle));
            header ("Location: /public_html/HTML/memberlist.html");
        }
        else
        {
            echo "nope.";
        }
}

    //Author: Lance Rainey
//Date: June 25, 2015
//Description: activates an inactive account
function activate($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];
        $query = "UPDATE `user` SET isActive = 1 WHERE username = '$user'";

        //Updates user to admin
        mysqli_query($db_handle, $query) or die(mysqli_error($db_handle));
        header ("Location: /public_html/HTML/memberlist.html");
}

    //Author: Lance Rainey
//Date: June 25, 2015
//Description: deactivates an active account
function deactivate($db_handle)
{
        //Grabs info from HTML
        $user = $_POST['user'];
        $query = "UPDATE `user` SET isActive = 0 WHERE username = '$user'";

        //Updates user to admin
        mysqli_query($db_handle, $query) or die(mysqli_error($db_handle));
        header ("Location: /public_html/HTML/memberlist.html");
}

Thank you all for the suggestions.