PHaeLiX
PHaeLiX

Reputation: 85

Simple form not submitting to database PHP MySQL

Banging my head on the wall trying to figure out why my information isn't being submitted into my MySQL database. All connection info is correct, I use 1 connection script and everything else is still working fine. PHP for submit is very basic while I'm testing, (i.e. sanitation and validation will come after I am submitting properly). But for whatever reason, this code will not submit. its the same code I'm using elsewhere so its only this one that is having issues.

PHP Code

<?php       

if(isset($_POST["schcreate"])){

error_reporting(E_ALL);

  $name = "";

  $location = "";

  $daystart = "";

  $shiftcount = "";

  $startdate = "";

 include_once("../php_includes/db_connect.php");

 $name = $_POST['schname'];

 $location = $_POST['schlocation'];

 $daystart = $_POST['schday'];

 $shiftcount = $_POST['schshiftcount'];

 $startdate = $_POST['schstart'];

 $sql = "INSERT INTO schinformations (schname, schlocation, schdaystart, schshiftcount, schstartdate) VALUES ('$name', '$location', '$daystart', '$shiftcount', '$startdate');";

$query = mysqli_query($db_connect, $sql); 

}
?>    

HTML Code

<form method="post" action="">
<div class="schq">Schedule Name:</div>
<div class="scha"><input type="text" id="schname" name="schname" /></div>
<div class="schq">Location:</div>
<div class="scha"><input type="text" id="schlocation" name="schlocation" /></div>
<div class="schq">Day of the week the schedule starts on:</div>
<div class="scha"><select id="schday" name="schday">
                <option value="default">Select </option>
                <option>Monday</option>
                <option>Tuesday</option>
                <option>Wednesday</option>
                <option>Thursday</option>
                <option>Friday</option>
                <option>Saturday</option>
                <option>Sunday</option>
            </select>
</div>
<div class="schq">Number of Shifts:</div>
<div class="scha"><input type="text" id="schshiftcount" name="schshiftcount" /></div>
<div class="schq">Start Date:</div>
<div class="scha"><input type="text" id="schstart" name="schstart" /></div>
<div class="schq">Next Step:</div>
<div class="scha"><input type="submit" id="schcreate" value="Create Shifts"/></div>
</form>

I have tried everything I can think of to see what's going on but I'm not getting any errors. Can anyone see what I am not here?

Thanks

Upvotes: 0

Views: 2016

Answers (2)

6opko
6opko

Reputation: 1760

Plain and simple:

<div class="scha"><input type="submit" id="schcreate" value="Create Shifts"/></div>

is supposed to be:

<div class="scha"><input type="submit" id="schcreate" name="schcreate" value="Create Shifts"/></div>

Upvotes: 0

omma2289
omma2289

Reputation: 54639

You're missing the name attribute for your submit button so there's no schcreate key in $_POST and your query doesn't run.

You can eliminate the need for the name attribute by checking $_SERVER['REQUEST_METHOD'] to get the request type, also you should use isset in each of your post values, so you could rewrite your code like this:

<?php       
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        error_reporting(E_ALL);
        include_once("../php_includes/db_connect.php");
        $name = isset($_POST['schname']) ? $_POST['schname'] : '';
        $location = isset($_POST['schlocation']) ? $_POST['schlocation'] : '';
        $daystart = isset($_POST['schday']) ? $_POST['schday'] : '';
        $shiftcount = isset($_POST['schshiftcount']) ? $_POST['schshiftcount'] : '';
        $startdate = isset($_POST['schstart']) ? $_POST['schstart'] : '';

        $sql = "INSERT INTO schinformations (schname, schlocation, schdaystart, schshiftcount, schstartdate) VALUES ('$name', '$location', '$daystart', '$shiftcount', '$startdate');";
        $query = mysqli_query($db_connect, $sql); 
    }
?>    

Upvotes: 2

Related Questions