简体   繁体   English

HTML表单无法通过PHP文件更新mySQL

[英]HTML form not updating mySQL through PHP file

I'm not quite sure what's going on because I'm following the same concept of implementation on this page as several other pages on the site. 我不太确定发生了什么,因为我在此页面上遵循与网站上其他几个页面相同的实现概念。 For some reason, this page will not update the database. 由于某些原因,此页面将不会更新数据库。 It throws no errors or anything, just doesn't update the user information. 它不会引发任何错误或任何错误,只是不会更新用户信息。

The HTML shows a text box and radio buttons at the top so the admin user can type a name and select whether to promote, demote, activate, or deactivate the account entered into the text box. HTML在顶部显示一个文本框和单选按钮,因此管理员用户可以键入名称并选择是升级,降级,激活还是停用在文本框中输入的帐户。 Once submitted, the code doesn't yell at me but instead does nothing. 提交后,代码不会对我大喊大叫,而是什么也不做。 I've tried several different ways of doing this but no matter what it's not updating the database. 我尝试了几种不同的方法来执行此操作,但是无论它不更新数据库是什么。

Here is the HTML file (shortened): 这是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);
        }
    }
?>

Functions from the PHP file: 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");
}

Can anyone tell me why the database won't take my update statements? 谁能告诉我为什么数据库不接受我的更新语句?

UPDATE: 更新:

With the error handling it is telling me that $db_handle is undefined and null from within the .php functions, however, there are over a dozen functions in the .php and all of them have $db_handle as a parameter and work. 通过错误处理,它告诉我$ db_handle在.php函数中未定义并且为null,但是.php中有十几个函数,并且所有这些函数都将$ db_handle作为参数并可以工作。 Even listmembers($db_handle) is used on this same .html and isn't giving errors. 甚至listmembers($ db_handle)都在同一.html上使用,并且没有给出错误。

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. 我通过在.php文件中创建一个新函数来检查需要运行的函数并将其传递给全局变量$ db_handle来解决了该问题。

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>
    </div>
</div>
</div>
<?php
}
function checkfunc()
{
    include_once ($_SERVER['DOCUMENT_ROOT'] . '/public_html/PHP/action.php');
    checkfunction($db_handle);
}
?>

PHP: 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. 谢谢大家的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM