[英]HTML form not updating mySQL through PHP file
我不太确定发生了什么,因为我在此页面上遵循与网站上其他几个页面相同的实现概念。 由于某些原因,此页面将不会更新数据库。 它不会引发任何错误或任何错误,只是不会更新用户信息。
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上使用,并且没有给出错误。
我通过在.php文件中创建一个新函数来检查需要运行的函数并将其传递给全局变量$ 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");
}
谢谢大家的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.