我的表单数据不会发布到我的phpmyadmin数据库中

时间:2015-01-22 22:03:36

标签: php mysql

其他声明"无效的表单详细信息,请确保您没有输入相同的主题两次始终显示" ,即使我还没有提交任何细节。此外,当我输入有效的详细信息时,没有数据提交到我的phpmyadmin数据库。没有显示任何错误。



<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); } 

//define page title
$title = 'Register Past Student';

?>
<html>
 
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Past Student</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

  


    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>


<form class="form-horizontal" method="post" action= "">

<fieldset>




<!-- Form Name -->
<legend> <a href= "memberpage.php"> Register A Past Student </a> </legend>
<h3> Enter the AS grades of students </h3>
<br>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="name">Student Name</label>  
  <div class="col-md-4">
  <input id="name" name="name" type="text" placeholder="First and Last Name" class="form-control input-md" required="">
    
  </div>
</div>

  <div class="form-group">  
  <label class="col-md-4 control-label" for="subject1">Subject 1</label>
  <div class="col-md-4">
    <select id="subject1" name="subject1" class="form-control" required="">
      <option value = "">-</option>
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <oprion value="Maths">Maths</oprion>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade1">Grade 1</label>
  <div class="col-md-4">
    <select id="grade1" name="grade1" class="form-control" required="">
      <option value = "">-</option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject2">Subject 2</label>
  <div class="col-md-4">
    <select id="subject2" name="subject2" class="form-control" required="">
      <option value = "">-</option>
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade2">Grade 2</label>
  <div class="col-md-4">
    <select id="grade2" name="grade2" class="form-control" required="">
      <option value = "">-</option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject3">Subject 3</label>
  <div class="col-md-4">
    <select id="subject3" name="subject3" class="form-control" required="">
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade3">Grade 3</label>
  <div class="col-md-4">
    <select id="grade3" name="grade3" class="form-control" required="">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject4">Subject 4</label>
  <div class="col-md-4">
    <select id="subject3" name="subject4" class="form-control" required="">
      <option value = "">-</option>
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>


<div class="form-group">
  <label class="col-md-4 control-label" for="grade4">Grade 4</label>
  <div class="col-md-4">
    <select id="grade1" name="grade4" class="form-control" required="">
      <option value = "">-</option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>

<!-- Multiple Radios (inline) -->
<div class="form-group">
  <label class="col-md-4 control-label" for="attendance">Attendance</label>  
  <div class="col-md-4">
  <input id="attendance" name="attendance" type="text" placeholder="enter in format 99.9 " class="form-control input-md" required="">
    
  </div>
</div>

<!-- Multiple Radios -->
<div class="form-group">
  <label class="col-md-4 control-label" for="gender">Gender</label>
  <div class="col-md-4">
  <div class="radio">
    <label for="gender-0">
      <input type="radio" name="gender" id="gender-0" value="Male" checked="checked">
      Male
    </label>
  </div>
  <div class="radio">
    <label for="gender-1">
      <input type="radio" name="gender" id="gender-1" value="Female">
      Female
    </label>
  </div>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" id="submit">Click Submit</label>
  <div class="col-md-4">
    <button id="singlebutton" type="submit" name="submit" class="btn btn-success">Submit</button>
  </div>
</div>

</fieldset>
</form>


<?php 
//require("includes/config.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

if (isset($_POST['name'])) { //checks if the name has posted value then inserts the data
        $name     = $_POST['name'];
        $subject1 = $_POST['subject1'];
        $grade1   = $_POST['grade1']; 
        $subject2 = $_POST['subject2'];
        $grade2   = $_POST['grade2'];
        $subject3 = $_POST['subject3'];
        $grade3   = $_POST['grade3'];
        $subject4 = $_POST['subject4'];
        $grade4   = $_POST['grade4'];
        $attendance = $_POST['attendance'];
        $gender     = $_POST['gender'];

        // <--------------Validation for form Post----------------------->

      

       
        $val = false;

        //validation to make sure the form isn't submitted blank and 2 of the same subjects haven't been chosen 
        if ($subject1 == $subject2 or $subject3 or $subject4) {

            $val = true;
          } 

          elseif ($subject2 == $subject1 or $subject3 or $subject4) {

                $val = true;

          }
            elseif ($subject3 == $subject1 or $subject2 or $subject4) {

                $val = true;
            }




            if ($val = false) {

          //inserting the data once validated
       
           
            $q        = "INSERT INTO paststudent (name,subject1,grade1,subject2,grade2,subject3,grade3,subject4,grade4,attendance,gender) VALUES (:name, :subject1,:grade1,:subject2,:grade2,:subject3,:grade3,:subject4,:grade4,:attendance,:gender);"; //sql statement add a colon intead of variables so pdo can prepare the statement and prevent any sql injections 
            $query    = $db ->prepare ($q);
            $results  = $query ->execute (array(  

                    ":name"     => $name,  //points to the variable 
                    ":subject1" => $subject1,
                    ":grade1"   => $grade1,
                    ":subject2" => $subject2,
                    ":grade2"   => $grade2,
                    ":subject3" => $subject3,
                    ":grade3"   => $grade3,
                    ":subject4" => $subject4,
                    ":grade4"   => $grade4,
                  ":attendance" => $attendanc,
                  ":gender"     => $gender
                  )); 
      
                        header("location:thanks.html");

                      } else {

                        echo"Invalid form details, make sure you haven't entered the same subject twice";
        }
      }
  

  
?> 

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你在这里没有比较,你正在分配:

if ($val = false) {

当您将值false分配给变量false时,这将始终评估为$val。因此,您永远不会进入实际尝试将信息存储在数据库中的部分。

虽然我没有得到它上面的逻辑(你应该把它减少到你发布在这里的绝对必要......),你可能想要:

if ($val !== false) {