php表单重定向到不同的文件而不是上传SQL查询

时间:2016-06-21 11:48:53

标签: php

我目前正在开发一个连接到足球联赛数据库的网页。我以一个表格的形式构建了页面,其中包含固定页眉,菜单,登录栏和一个单元格(主页面),显示菜单中选择的页面(例如,选择“结果”时所有游戏的结果)菜单)。

一切工作都很顺利,直到我们到达主页中显示的页面有两个与数据库链接的形式。每次,其中一个提交按钮将我的页面重定向到菜单中的另一个位置而不执行SQL查询...奇怪的是,如果我在网页上单独加载相关文件,则执行所有操作...但是当它是一个更大的整体的一部分,它不能正常工作。

以下是显示主要内容的单元格的代码:

<?php
$id = $_REQUEST["id"];
$lang=$_REQUEST["lang"];
switch($id)
{
case 1:
switch($lang){
    case pl: include "Main/PL/index.php"; break;
    case en: include "Main/EN/index.php"; break;
    default: include "Main/PL/index.php"; break;
}; break;
case 2:
switch($lang){
    case pl: include "News/PL/index.php"; break;
    case en: include "News/EN/index.php"; break;
    default: include "News/PL/index.php"; break;
}; break;
case 3:
switch($lang){
    case pl: include "Tables/PL/index.php"; break;
    case en: include "Tables/EN/index.php"; break;
    default: include "Tables/PL/index.php"; break;
}; break;
case 4:
switch($lang){
    case pl: include "Results/PL/index.php"; break;
    case en: include "Results/EN/index.php"; break;
    default: include "Results/PL/index.php"; break;
}; break;
case 5:
switch($lang){
    case pl: include "Fixtures/PL/index.php"; break;
    case en: include "Fixtures/EN/index.php"; break;
    default: include "Fixtures/PL/index.php"; break;
}; break;
case 6:
switch($lang){
    case pl: include "Teams/PL/index.php"; break;
    case en: include "Teams/EN/index.php"; break;
    default: include "Teams/PL/index.php"; break;
}; break;
case 7:
switch($lang){
    case pl: include "Contact/PL/index.php"; break;
    case en: include "Contact/EN/index.php"; break;
    default: include "Contact/PL/index.php"; break;
}; break;
default: 
switch($lang){
    case pl: include "Main/PL/index.php"; break;
    case en: include "Main/EN/index.php"; break;
    default: include "Main/PL/index.php"; break;
}; break;
}
?>

以下是一个表现不佳的表格示例 - 用于添加和删除团队中的玩家的表单:

<!DOCTYPE html>
<html>
<body>
<!-- First form for adding a player -->
<h3>Add a Player to Your Team</h3><br>
<form method='POST' action='index.php?id=6&lang=en'>First name: <input type=\"text\" name=\"imie\"><br>
Last name: <input type=\"text\" name=\"nazwisko\"><br>
<input type='submit' name='dodaj' value='Add'></form><br>
<?php
//what happens when button add player is selected   
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (isset($_POST['dodaj'])){
//checks if data is input
if (empty($_POST['imie']) || empty($_POST['nazwisko'])){
echo "Provide the name of the player!";
}
else {
//verify that the player has not already been added
$imie=$_POST['imie'];
$nazwisko=$_POST['nazwisko'];
$sql="SELECT * FROM `Zawodnik` WHERE `Zawodnik_Imie`='$imie' AND `Zawodnik_Nazwisko`='$nazwisko'";
$result=mysqli_query($conn, $sql);
$czyjest=mysqli_num_rows($result);
    if ($czyjest >= 1){
        echo "The player has already been added!";
    }
    else {
//add the player
$sql="INSERT INTO `Zawodnik`(`Zawodnik_Imie`, `Zawodnik_Nazwisko`, `Druzyna_Id_Druzyna`) VALUES ('$imie', '$nazwisko', '$iddruzyna')";
$result=mysqli_query($conn, $sql);
if (!$result){
    die ("Could not enter data ".mysqli_error($conn));
}
else {echo "Player added!";}
}
}
}
}
?>
<!-- Second form for deleting a player -->
<h3>Remove Player</h3><br>
<form method='POST' action='index.php?id=5&lang=en'>ID nr: <input type='number' name='id'><br>
<input type='submit' name='usun' value='Delete'></form><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (isset($_POST['usun'])){
//verify if the player ID has been input
if (empty($_POST['id'])){
    echo "Provide Player ID!";
}
else {
//Delete player
    $id=$_POST['id'];
    $sql="DELETE FROM `Zawodnik` WHERE `Id_Zawodnik`=$id AND `Druzyna_Id_Druzyna`=$iddruzyna";
    $result=mysqli_query($conn, $sql);
if (!$result){
    die ("Could not delete data ".mysqli_error($conn));
}
else {
    $sql="SELECT * FROM `Zawodnik` WHERE `Id_Zawodnik`=$id";
    $result=mysqli_query($conn, $sql);
    $czyusuniety=mysqli_num_rows($result);
    if($czyusuniety<1){
    echo "Player removed!";}
    else {
        echo "You can't remove a player from a different team!";
    }
}
}
}
}
}

mysqli_close($conn); 

?>
</body>
</html>    

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

看起来你在表单操作属性中添加了错误的页面ID(一旦它是6,一旦它是5)。

要添加,您需要:

<form method='POST' action='index.php?id=6&lang=en'>

和删除:

<form method='POST' action='index.php?id=5&lang=en'>

哦,你也使用相同的名字进行GET和POST,所以$ _REQUEST [&#39; id&#39;]实际上会有发布的值而不是url参数。