试图检查$ _POST是否存在

时间:2014-03-17 04:31:48

标签: php

我真的不知道该怎么称呼这个问题,但我有这个代码:

    <?php

include 'connect.php';
include 'functions.php';

$u_id = $_GET['u_id'];
$u_name = $_GET['username'];
$type = $_GET['type'];
$post1 = $_POST[''];

if ($type == 'a') {
    mysql_query("UPDATE users SET type='b' WHERE id='$u_id'");
    header('location: admin.php?type=user');
} else if ($type == 'b') {
    mysql_query("UPDATE users SET type='d' WHERE id='$u_id'");
    header('location: admin.php?type=user');
} else if ($type == 'd') {
    mysql_query("UPDATE users SET type='a' WHERE id='$u_id'");
    header('location: admin.php?type=user');
} else {
    mysql_query("UPDATE users SET type='d' WHERE id='$u_id'");
    header('location: admin.php?type=user');
}

if ($user_level >= 2) {
    mysql_connect("...", "...", "...");
    mysql_select_db("dojo");
    mysql_query("UPDATE stats SET kills='0', deaths='0', wins='0', money='0' WHERE name='$username'");
    header('location: profile');
}

mysql_connect("...", "...", "...");
mysql_select_db("adminpanel");
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
$row = mysql_fetch_array($result);
$private = $row['private'];

if ($private == 'true') {
    mysql_query("UPDATE users SET private='false' WHERE username='$username'");
    header('location: profile');
    exit();
} else if ($private == 'false') {
    mysql_query("UPDATE users SET private='true' WHERE username='$username'");
    header('location: profile');
    exit();
}
?>

但事实是,当我点击页面上的一个按钮时,它会完成所有这些操作,所以我怎样才能确保它只执行其中一个,例如,让我说我点击了一个我的个人资料上的按钮说Make Profile Private,然后当我这样做时它只会执行

   if ($private == 'true') {
    mysql_query("UPDATE users SET private='false' WHERE username='$username'");
    header('location: profile');
    exit();
} else if ($private == 'false') {
    mysql_query("UPDATE users SET private='true' WHERE username='$username'");
    header('location: profile');
    exit();
}

我该怎么做?

编辑:

我已经在options.php文件中稍微改了一下,但我仍然无法使其正常工作..我已将按钮的ID设置为resetstats和changeprivacy

    <?php

include 'connect.php';
include 'functions.php';

if (isset($_POST['resetstats'])) {
$type = $_GET['type'];
$u_name = $_GET['username'];
$u_id = $_GET['u_id'];
if ($user_level >= 2) {
    mysql_connect("...", "...", "...");
    mysql_select_db("dojo");
    mysql_query("UPDATE stats SET kills='0', deaths='0', wins='0', money='0' WHERE name='$username'");
    header('location: profile');
}
}

if (isset($_POST['changeprivacy'])) {
mysql_connect("...", "...", "...");
mysql_select_db("adminpanel");
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
$row = mysql_fetch_array($result);
$private = $row['private'];
$type = $_GET['type'];
$u_name = $_GET['username'];
$u_id = $_GET['u_id'];
if ($private == 'true') {
    mysql_query("UPDATE users SET private='false' WHERE username='$username'");
    header('location: profile');
} else if ($private == 'false') {
    mysql_query("UPDATE users SET private='true' WHERE username='$username'");
    header('location: profile');
}
}
?>

2 个答案:

答案 0 :(得分:0)

你可以这样做

<form>
.
.
.
    <input type="hidden" name="mode" value="resetstats">
</form>



<form>
.
.
.
    <input type="hidden" name="mode" value="changeprivacy">
</form>

然后

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') // if form is posted
{
    switch ($_POST['mode'])
   {
       case "resetstats":
           echo "your code to reset stats here";
       break;

       case "changeprivacy":
           echo "your code to change privacy here";
       break;
    }

}

?>

答案 1 :(得分:0)

为什么你不这样做?我在网址上添加了操作

if ($user_level >= 2){
    if (($kills || $deaths) == 0) {         
        echo "<a class='btn btn-danger' disabled='disabled' href='#'>Reset Your Stats</a>";
    } 
    else{       
        echo "<a id='resetstats' class='btn btn-danger' href='options.php?action=resetstats&username=$username'>Reset Your Stats</a>"; 
    } 
}

和您的options.php

 <?php

include 'connect.php';
include 'functions.php';

$action = $_GET['action'];

if ($action == 'resetstats') {
$type = $_GET['type'];
$u_name = $_GET['username'];
$u_id = $_GET['u_id'];
if ($user_level >= 2) {
    mysql_connect("...", "...", "...");
    mysql_select_db("dojo");
    mysql_query("UPDATE stats SET kills='0', deaths='0', wins='0', money='0' WHERE name='$username'");
    header('location: profile');
}
}

if ($action == 'changeprivacy') {
mysql_connect("...", "...", "...");
mysql_select_db("adminpanel");
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
$row = mysql_fetch_array($result);
$private = $row['private'];
$type = $_GET['type'];
$u_name = $_GET['username'];
$u_id = $_GET['u_id'];
if ($private == 'true') {
    mysql_query("UPDATE users SET private='false' WHERE username='$username'");
    header('location: profile');
} else if ($private == 'false') {
    mysql_query("UPDATE users SET private='true' WHERE username='$username'");
    header('location: profile');
}
}
?>