doulu8847 2014-05-27 19:55
浏览 75
已采纳

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

I am currently programming a page in which when the submit button is pressed it runs an sql script to add a form into the database. However when the submit button is pressed the page just reloads and the script is not run. The source code for the page is shown below

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条回答 默认 最新

  • doutang8098 2014-05-27 20:09
    关注

    As @relentless and @MahasishShome already noticed, your update is inside

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

    But there is no field named description in the form you submit.

    You must either insert a (hidden?) field named description or test for another existing field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?