Hello. I have text fields with data that is not being inserted into my table 'Election'.

The Election table has these fields: ID (primary key and auto-increment), title, open_datetime, close_datetime, department, adminID, active (default is 'N')

I checked to make sure all the field names are the same. Everything works fine but nothing is showing up in my database!

Here is my code: See if you can find the culprit:

<?
session_start();
if(!isset($_SESSION['user_admin'])) {
header("Location: index.php");
exit();
}

include '../dbc.php';

/**establish connection*/
connect();

if($_POST['doSubmit'] == 'Create')
{
  

/**Checking if any of the fields are date fields are blank*/
/****************************************Sets session variables *****************************************************/
  $adminID = $_SESSION['user_admin']; //extracts admin from the session to associate the election with the admin creating it
  $title = $_POST['title'];
  $department = $_POST['department'];
  $open_year = $_POST['open_year'];
  $open_month = $_POST['open_month'];
  $open_day = $_POST['open_day'];
  $open_hour = $_POST['open_hour'];
  $open_minutes = $_POST['open_minutes'];
  $open_AM_PM = $_POST['open_AM_PM'];
  $open_datetime = $open_year . "-" . $open_month . "-" . $open_day . " " . $open_hour . ":" . $open_minutes . ":00"; 
  $close_datetime = $close_year . "-" . $close_month . "-" . $close_day . " " . $close_hour . ":" . $close_minutes . ":00"; 

      function isValidDateTime($dateTime)
{
    if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
        if (checkdate($matches[2], $matches[3], $matches[1])) {
            return true;
        }
    }

    return false;
}//end of isValidDateTime() 


  function checkFields($title, $department)
{
            
    if(($title||$department)== "")
	{
	$msg = urlencode("Fields must not be blank. Please fill in all required fields. ");
	header("Location:newuser.php?msg=$msg");
	exit();

	}
	else
		return;	

}//end of checkFields() 

function checkopenAMPM($open_AM_PM){
//takes the PM and AM and convert the hours into XX format

if($open_AM_PM == "PM")
    {
      if ($open_hour= "12") 
	$open_hour = $open_hour + 12; 
      	return $open_hour;
    }
if($open_AM_PM == "AM")
    {
      if($open_hour == "12")
	$open_hour = 0;
	return $open_hour;
    }
}

function checkcloseAMPM($close_AM_PM)
{
if($close_AM_PM == "PM")
    {
      if ($close_hour= "12") 
	$close_hour = $close_hour + 12; 
      return $close_hour;
    }
if($close_AM_PM == "AM")
    {
      if($close_hour == "12")
	$close_hour = 0;
	return $close_hour;
    }
} //end of checkcloseAMPM

checkcloseAMPM($close_AM_PM);
checkopenAMPM($open_AM_PM); 
 
  if(((isValidDateTime($open_datetime))||(isValidDateTime($close_datetime)))== false){

	$date_msg = urlencode("ERROR: Date is invalid.");
	header("Location: newuser.php?msg=$date_msg");
	exit();
 	 }

checkFields($title, $department);  //check fields to see if they are blank. If any are blank, so error message.

  mysql_query("INSERT INTO Election (title, department, open_datetime, close_datetime, adminID, active)
                         VALUES('$title','$department','$open_datetime', '$close_datetime','$adminID', 'N')" or die(mysql_error()));

echo "User created....done"; 
}
?>

<HTML>
<HEAD>
    <LINK href="style.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<div id="banner"><h1><center><div style="width:600px;height:65px;font-size:20pt;border:10px double black;">
  <b>Create New Election</b><br/><font size=5pt>Step 1: General Information</font>
</div></center>
</h1>
</div>

<div id="container">
  <div id="container2">
  <div id="navigation"><u1><li><a href="#">Nav item 1</a></li></u1></div>
  <div id="more">
<h3></h3>
</div>
  <div id="content"><h2>

 <p>

<?
/******************** ERROR MESSAGES*************************************************
          This code is to show error messages
**************************************************************************/
  if (isset($_GET['msg'])) {
    $msg = mysql_real_escape_string($_GET['msg']);
    echo "<div class=\"msg\">$msg</div>";
  	}
/******************************* END ********************************/
?></p>

<p>
          <?
 /******************** ERROR MESSAGES*************************************************
          This code is to show error messages
 **************************************************************************/
if (isset($_GET['date_msg'])) {
  $date_msg = mysql_real_escape_string($_GET['date_msg']);
  echo "<div class=\"date_msg\">$date_msg</div>";
}
/******************************* END ********************************/
?></p>

  <h2>Step 1: General Information</h2>
<form name="form1" method="post" action="newuser.php">
 <i>Required fields are marked with an asterisk(<span class= "required">*</span>)</i><br/><br/>
<p>Title of election <span class = "required">*</span>:       
&nbsp; &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
    <input name="title" type="text" id="title">
  </p>
  <p>Department/Organization <span class = "required">*</span>:  &nbsp;
    <input name="department" type="text" id="department">
  <span class = "example">Example: Computer Science Department, Student Body Council</span>

  </p>
  <p>Number of offices/positions <span class = "required">*</span>:
<select name="position">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
  <span class ="example">Example: President, Vice President, Secretary will be a total of 3 positions</span>
</p>


  <p>Poll opening date/time <span class = "required">*</span>: &nbsp; &nbsp; &nbsp; &nbsp;
<select name="open_year">
<option value="2009" selected="selected">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>

<select name="open_month">
<option value="01" selected="selected">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sept</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>

</select>

<select name="open_day">
<option value="01" selected="selected">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
  &nbsp; &nbsp;&nbsp; &nbsp;

<select name="open_hour">
<option value="01" selected="selected">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>

</select>

<select name="open_minutes">
<option value="00" selected="selected">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>

<select name="open_AM_PM">
<option value="AM" selected="selected">AM</option>
<option value="PM">PM</option>
</select>




</p>
  
<p>Poll closing date/time <span class = "required">*</span>: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<select name="close_year">
<option value="2009" selected="selected">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>

<select name="close_month">
<option value="01" selected="selected">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sept</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>

</select>

<select name="close_day">
<option value="01" selected="selected">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
  &nbsp; &nbsp;&nbsp; &nbsp;

<select name="close_hour">
<option value="01" selected="selected">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>

</select>

<select name="close_minutes">
<option value="00" selected="selected">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>

<select name="close_AM_PM">
<option value="AM" selected="selected">AM</option>
<option value="PM">PM</option>
</select>




</p>
  
  <p>
    <input name="doSubmit" type="submit" id="doSubmit" value="Create"> 
   </p>
</form>
  <p>&nbsp;</p>
</h2><p>
<?  nextStepButton_goToStep2(); cancelButton(); ?></p></div>
  <div id="cleardiv"> </div>
  </div>
</div>
<div id="footer"><u1><li><a href="#">Footer item 1</a></li>
</u1></div>


</BODY>
</HTML>

acutally. i figured it out. I fixed the logic a bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.