点击提交按钮时页面重新加载

时间:2014-05-27 19:55:14

标签: php html sql forms submit

我正在编写一个页面,当按下提交按钮时,它会运行一个sql脚本将表单添加到数据库中。但是,当按下提交按钮时,页面只会重新加载并且脚本不会运行。该页面的源代码如下所示

addResults.php:

<?php

if(isset($_POST['description'])){
    if(!isset($_POST['iscorrect']) || $_POST['iscorrect'] == ""){
        echo "Sorry, data is missing. Please press back and try again.";
        exit();
    }
    if(!isset($_POST['type']) || $_POST['type'] == ""){
        echo "Sorry, there was an error. Please press back and try again";
        exit();
    }
    require_once("scripts/connect_db.php");

    $team1 = $_POST['team1'];
    $team2 = $_POST['team2'];
    $score1 = $_POST['score1'];
    $score2 = $_POST['score2'];
    $year = $_POST['year'];
    $round = $_POST['round'];

    $type = $_POST['type'];
    $type = preg_replace('/[^a-z]/', "", $type);
    $team1 = strip_tags($team1);
    $team1 = mysql_real_escape_string($team1);
    $team2 = strip_tags($team2);
    $team2 = mysql_real_escape_string($team2);
    $score1 = strip_tags($score1);
    $score1 = mysql_real_escape_string($score1);
    $score2 = strip_tags($score2);
    $score2 = mysql_real_escape_string($score2);
    $year = strip_tags($year);
    $year = mysql_real_escape_string($year);
    $round = strip_tags($round);
    $round = mysql_real_escape_string($round);


    if($type == 'mr'){
    if((!$team1) || (!$team2) || (!$score1) || (!$score2) || (!$year) || (!$round)){
        echo "Sorry, All fields must be filled in. Press back and try again.";
        exit();
        }
    }
    if($type == 'mr'){
        $previousId = mysql_insert_id();
        mysql_query("UPDATE questions SET question_id='$previousId' WHERE id='$previousId' LIMIT 1")or die(mysql_error());
    }

    if($type == 'mr'){
    $sql2 = mysql_query("INSERT INTO questions (ID, Team1, Team2, Score1, Score2, Year, Round) VALUES ('$previousId', '$team1', '$team2', '$score1', '$score2', '$year', '$round')")or die(mysql_error());
    $message = 'Thank You, Your Result Has Been Added';
    header('location: addResults.php?msg='.$message.'');
    exit();
    }
$message = "";
if(isset($_GET['msg'])){
    $message = $_GET['msg'];
}

}
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add a Question</title>
<script>
function showDiv(at1,at2){
    document.getElementById(at1).style.display = 'block';
    document.getElementById(at2).style.display = 'none';
}
</script>

<style type="text/css">
.content{
    margin-top:48px;
    margin-left:auto;
    margin-right:auto;
    width:780px;
    border:#333 1px solid;
    border-radius:12px;
    -moz-border-radius:12px;
    padding:12px;
    text-align:center;

    background-image: url(/images/grass.jpg)
}
body { background: url("http://www.environ.org.uk/wp-content/uploads/2013/04/football_pitch_1129.jpg") }
</style>
</head>

<body>
   <div style="width:700px;margin-left:auto;margin-right:auto;text-align:center;">
   </div>
 <div class="content" id="mr">
    <h3>Match Result</h3>
    <form action="addResults.php" name="addMcQuestion" method="post">

    <strong>Please Enter the First Team</strong>
        <br />
        <input type="text" id="team1" name="team1">&nbsp;
      <br />
    <br />
    <strong>Please Enter the Second Team</strong>
        <br />
        <input type="text" id="team2" name="team2">&nbsp;
      <br />
    <br />
    <strong>Please Enter the Score for Team 1</strong>
        <br />
        <input type="text" id="score1" name="score1">&nbsp;
      <br />
    <br />
    <strong>Please Enter the Score for Team 2</strong>
        <br />
        <input type="text" id="score2" name="score2">&nbsp;
      <br />
    <br />
    <strong>Please Enter the Year of the Match</strong>
        <br />
        <input type="text" id="year" name="year">&nbsp;
      <br />
    <br />
    <strong>Please Enter the Round of the Tournament</strong>
        <br />
        <input type="text" id="round" name="round">&nbsp;
      <br />
    <br />
    <br />
   <input type="hidden" value="mr" name="type">
    <input type="submit" value="Add To Quiz">
    </form>
    </form><form action="menu.php"><input type="submit" value="Return to Menu"></form>
 </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

由于@relentless和@MahasishShome已经注意到,您的更新在

之内
if(isset($_POST['description'])) {
// update ...
}

但您提交的表单中没有名为description的字段。

您必须插入名为description的(隐藏?)字段或测试另一个现有字段。