Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

How comes my form data won't get inserted to my database

I am trying to insert past student data into my database, but it doesn't appear to work.

I have the paststudent page with all the forms i have used and my sql statement. My configuration file works, as no errors are produced and i can register users with no problem.

<?php 
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">
<form action ="paststudent.php" method ="post">
<fieldset>




<!-- Form Name -->
<legend> <a href= "memberpage.php"> Register A Past Student </a> </legend>




<!-- 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">
      <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="grade1">Grade 1</label>
  <div class="col-md-4">
    <select id="grade1" name="grade1" class="form-control">
      <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">
      <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">
      <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">
      <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">
      <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">
      <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">
      <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="attendanc">Attendance %</label>
  <div class="col-md-4"> 
    <label class="radio-inline" for="attendanc-0">
      <input type="radio" name="attendanc" id="attendanc-0" value="99-100" checked="checked">
      99-100
    </label> 
    <label class="radio-inline" for="attendanc-1">
      <input type="radio" name="attendanc" id="attendanc-1" value="97-98">
      97-98
    </label> 
    <label class="radio-inline" for="attendanc-2">
      <input type="radio" name="attendanc" id="attendanc-2" value="95-96">
      95-96
    </label> 
    <label class="radio-inline" for="attendanc-3">
      <input type="radio" name="attendanc" id="attendanc-3" value="">
      93-94
    </label> 
    <label class="radio-inline" for="attendanc-4">
      <input type="radio" name="attendanc" id="attendanc-4" value="">
      91-92
    </label> 
    <label class="radio-inline" for="attendanc-5">
      <input type="radio" name="attendanc" id="attendanc-5" value="">
      90 or less
    </label>
  </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" for="singlebutton">Click Submit</label>
  <div class="col-md-4">
    <button id="singlebutton" name="submit" class="btn btn-success">Submit</button>
  </div>
</div>

</fieldset>
</form>
</form>
</html>
<?php
    if (isset($_POST['submit'])) {


        $sql = "INSERT INTO paststudent (name,subject1,grade,subject2,grade2,subject2,grade2,subject3,grade3,subject4,grade4,attendance,gender) VALUES ('$_POST[name]','$_POST[subject1]','$_POST[grade1]','$_POST[subject2]','$_POST[grade2]','$_POST[subject3]','$_POST[grade3]','$_POST[subject4]','$_POST[grade4]','$_POST[attendance]','$_POST[gender]')";

    }
     //header('location:thanks.html');



?>

1 Answer

Ricky Catron
Ricky Catron
13,023 Points

Your form is not interacting with the database from what I can tell. It looks like you are just saving the SQL Query to a string but never executing it. You will need to create a database connection and run your query against the database for it to have any effect. Right now it is just a string variable.

Goodluck! --Ricky