php - Page Reloads when submit button is hit

534

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 >
  </div>
 <div 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>
464

Answer

Solution:

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

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

But there is no field nameddescription in the form you submit.

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

People are also looking for solutions to the problem: php - update database rows by $_POST data with key as id for row

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.