evan
evan

Reputation: 964

Form data is submitting, but isn't going into mySQL

My db connection is correct, I am able to select and echo out rows of my table. However, my form data isn't updating my database. I'm trying to submit the form on the same page, so I left the action blank. Here is my php:

<?php
if (isset($_POST['submit'])) {

    $con=mysqli_connect("localhost","hey","password!","dbname");
    $sql="INSERT INTO StudentList (StudentNum, LastName, FirstName, Address, City, State, Zip, Balance, FirstTermAttended)
    VALUES('$_POST[inputID]','$_POST[inputLast]','$_POST[inputFirst]','$_POST[inputAddress]','$_POST[inputCity]','$_POST[inputState]','$_POST[inputZip]','$_POST[inputBalance]','$_POST[inputTerm]')";
    echo "It worked";
}
?>

and here my form:

   <form class="form-horizontal" role="form" method="post" action="">
                <div class="form-group">

                    <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputID" placeholder="Student ID Number">
                    </div>
                 </div>


                <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputLast" placeholder="Last Name">
                    </div>
                 </div>

                <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputFirst" placeholder="First Name">
                    </div>
                </div>

                 <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputAddress" placeholder="Street Address">
                    </div>
                </div>

                 <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputCity" placeholder="City">
                    </div>
                </div>

                 <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputState" placeholder="State">
                    </div>
                </div>

                  <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputZip" placeholder="Zip">
                    </div>
                </div>

                <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputBalance" placeholder="Current Balance">
                    </div>
                </div>

                 <div class="form-group">
                     <div class="col-sm-offset-1 col-sm-10">
                        <input type="text" class="form-control" id="inputTerm" placeholder="First Term Attended">
                    </div>
                </div>

                  <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <input type="submit" value="Save Student Information" id="submit" name="submit" class="btn btn-primary">
        </div>


            </form>

I also have this in my header, could it be screwing things up?

<?php
// Create connection
$con=mysqli_connect("localhost","hey","password!","dbname");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

Upvotes: 0

Views: 143

Answers (5)

Monirul Islam
Monirul Islam

Reputation: 935

Your code is missing mysqli_query and also you need to assign name attribute to your input field. See Sample Example from here http://www.w3schools.com/php/php_mysql_insert.asp

Upvotes: 3

Owais
Owais

Reputation: 21

Assign id or class in your form instead of using div in your form. For further follow this example http://people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html

Upvotes: 0

Snehal S
Snehal S

Reputation: 875

You have only written the query. You have to execute the query with "mysqli_query".

Also specify "name" attribute to input elements. so that you can assess them in $_POST.

Upvotes: 0

user3106988
user3106988

Reputation: 101

change id="name" into name="name" in <input id="">

Upvotes: 0

Connor Peet
Connor Peet

Reputation: 6265

Your <input> fields require "name" attributes. It's the names that are used in the POST body - not the IDs! You should also look at prepared statements - you're open to injection at the moment. Also, it doesn't appear that you're actually executing the queries. Read what I link - it shows execution, too!

Upvotes: 0

Related Questions