Form processing and errors with inputs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

Form processing and errors with inputs

Post by redrocket1855 »

http://omega.uta.edu/~sdb9437/ScooterBr ... venue.html

My problem is when you add a venue using the form I created it does not allow me to because of an error with "time" and "date"... Feel free to add a TEST venue to see the error url is above..Also I am new to PHP so any other comments or suggestions will be helpful.... The php and html is below,

Code: Select all

      
[color=#0000FF]<?php # add_venue.php
 
// Include the database connection script&#058;
require_once('mysql.inc.php');
 
// Array for handling errors:
$errors = array();
  
// Validate the required data;
if (!empty($_POST['venue'])) {
  $ve = mysql_real_escape_string($_POST['venue'], $dbc);
} else {
  $errors[] = 'venue';
}
 
 
if (!empty($_POST['thisdate'])) {
  $da = mysql_real_escape_string($_POST['thisdate'], $dbc);
} else {
  $errors[] = 'date';
}
 
if (isset($_POST['city_id']) && is_numeric($_POST['city_id']) && ($_POST['city_id'] > 0)) {
  $cid = (int) $_POST['city_id'];
} else {
  $errors[] = 'city';
}
 
if (isset($_POST['thistime']) && is_numeric($_POST['thistime']) && ($_POST['thistime'] > 0)) {
  $ti = (int) $_POST['thistime'];
} else {
  $errors[] = 'time';
}
 
if (!$errors) { // If no errors, add the venue.
 
  // Run the query:
 
  /* ######################
    COMPOSE the DATE
    Note that each component of the date should be validated before you compose them for the datebase
   ######################## */
 
    $thisDate = "{$_POST['M']}/{$_POST['D']}/{$_POST['Y']}";
 
  $q = "INSERT INTO venues VALUES (NULL, '$cid', '$thisDate', '$ti', '$ve')";
 
  
 
  echo $q;
//  $r = mysql_query($q, $dbc);
  
  // Check that the query worked:
/*
    if (mysql_affected_rows($dbc) == 1) {
  
    echo '<p><strong>The Concert Information has been added.</strong></p>';
      
  } else { // Query failure.
    echo '<p class="error">The Concert Information could not be added due to a system error.</p>';
  }
*/
} else { // Errors!
  echo '<p>The following errors occurred:</p><ul class="error">';
  
  // Print each error:
  foreach ($errors as $e) {
    echo "<li>Please enter a valid $e.</li>\n";
  }
  
  echo '</ul>';
  
}
 
// Close the database connection.
//mysql_close($dbc);
 
?>
</body>
[/color]

and the HTML

Code: Select all

<div id="results"></div>
<form action="add_venue.php" method="post" id="venue_form">
<p><label class="title" id="venue_label">Venue <input type="text" id="venue" name="venue" /></label> </p>
 
 
<p>
<label class="title" id="date_label">Month
    <select name="M">
        <option>January</option>
        <option>February</option>
        <option>March</option>
        <option>April</option>
        <option>May</option>
        <option>June</option>
        <option>July</option>
        <option>August</option>
        <option>September</option>
        <option>October</option>
        <option>November</option>
        <option>December</option>
    </select>
</label> 
 
<label class="title" id="date_label">Day
    <select name="D">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
        <option>9</option>
        <option>10</option>
        <option>11</option>
        <option>12</option>
        <option>13</option>
        <option>14</option>
        <option>15</option>
        <option>16</option>
        <option>17</option>
        <option>18</option>
        <option>19</option>
        <option>20</option>
        <option>21</option>
        <option>22</option>
        <option>23</option>
        <option>24</option>
        <option>25</option>
        <option>26</option>
        <option>27</option>
        <option>28</option>
        <option>29</option>
        <option>30</option>
        <option>31</option>
    </select>
</label>
 
<label class="title" id="date_label">Year
    <select name="Y">
        <option>2009</option>
        <option>2010</option>
        <option>2011</option>
        <option>2012</option>
        <option>2013</option>
 </select>
</label>
</p>
 
<p><label class="title" id="time_label">Time <input type="text" id="time" name="thistime" /></label> </p>
 
<p><label class="title" id="city_id_label">City <select id="city_id" name="city_id">
<option value="1">College Station </option>
<option value="2">Conroe</option>
<option value="3">Cypress</option>
<option value="4">Freiheit</option>
<option value="5">Humble</option>
<option value="6">Hunter</option>
<option value="7">Magnolia</option>
<option value="8">Mont</option>
<option value="9">Montgomery</option>
<option value="10">Navasota</option>
<option value="11">Porter</option>
<option value="12">Spring</option>
<option value="13">Texas</option>
<option value="14">Tomball</option>
<option value="15">Old Town Spring</option>
<option value="16">Webster</option>
<option value="17">Willis</option>
<option value="18">The Woodlands</option>
</select></label> </p>
 
<p><input name="add" type="submit" value="Add" /></p>
 
</form>
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Form processing and errors with inputs

Post by abushahin »

hey few things, why don't you echo the date and time variables that you have to see whats being sent, then check it against what the database expects, you can have a datetime type in your db as a field instead of two separate fields one for date and another for time that'll do the same thing.
you can specify the received variables (date and time) in a format that the db accepts as datetime probably something like this:

Code: Select all

$newdate = Date ('12/25/2005 01:00PM')
so if you are receiving your date in three parts:

Code: Select all

$thisDate = "{$_POST['M']}/{$_POST['D']}/{$_POST['Y']}";
$newdate = Date ('$thisDate, 01:00PM')
 
then maybe echo the $newDate to see if its working! hope that helps
abu
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Form processing and errors with inputs

Post by superdezign »

  1. $errors is an array, even if it is an empty array. I'm not sure if using "!$errors" is equivalent to "empty($errors)," but I do know that using empty() is the accepted form of doing what you're doing.
  2. Labels are supposed to use the "for" attribute to link them with their input elements. The "for" attribute should have a value equivalent to the "name" attribute of the linked input element.
  3. You shouldn't do any for processing unless the form has been submitted. This can be checked by checking if $_POST is empty or not.
  4. Why would a time be numeric? Aren't you expecting a colon, and possibly AM or PM?
  5. $_POST['thisdate'] doesn't exist.
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

Re: Form processing and errors with inputs

Post by redrocket1855 »

I have changed the date and time "type" in Mysql database from "varchar" (on both) to "date" and "time." I understand this but am confused how to implement this in to the php. From what I have been reading there are ways in PHP to define the variable as "date" and "time". Does anyone know any references that might help me with this problem?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Form processing and errors with inputs

Post by superdezign »

MySQL has a single date and time column type, DATETIME. DATETIME is basically a string sent in this format:

Code: Select all

YYYY-MM-DD HH:MM:SS
For the date() function, this is the format:

Code: Select all

Y-m-d H:i:s
In order to make use of this, use values in your forms. Then you can use the mktime() function to create the date, and send it to the date() function.

Since I have time on my hands, here's an example:

Code: Select all

<?php
/**
 * Build a list of <option> elements from an array
 * @param array Options in "value => name" format
 * @return string HTML <option> elements
 */
function buildOptionList($options) {
  if (!is_array($options)) {
    return '';
  }
  $output = '';
  foreach ($options as $value => $name) {
    $output .= '<option value="' . $value . '">' . $name . '</option>' . "\n";
  }
  return $output;
}
 
/**
 * Validate a selected option using an array
 * @param string Selected option's $_POST array key name
 * @param array Valid options
 * @return boolean Whether the option is valid
 */
function validateSelection($selection, $options) {
  if (empty($_POST[$selection])) {
    return false;
  }
  foreach ($options as $value => $name) {
    if ($value == $_POST[$selection]) {
      return true;
    }
  }
  return false;
}
 
// You can use loops (and the date() function for $months) to fill these arrays
$months = array(1 => 'January', 'February', 'March', 'April', ... , 'December');
$days = array(1 => 1, 2, 3, ... , 30, 31);
$years = array(2009 => 2009, 2010, 2011, ...);
$hours = array(1 => 1, 2, 3, ... , 23, 24);
$minutes = array(0 => '00', '01', '02', ... , '58', '59');
$seconds = $minutes;
 
// Process the form, altering $isValid to be false if any selections are invalid
$isValid = true;
if (!empty($_POST)) {
  $isValid = validateSelection('month', $months) ? $isValid : false;
  $isValid = validateSelection('day', $days) ? $isValid : false;
  $isValid = validateSelection('year', $years) ? $isValid : false;
  $isValid = validateSelection('hour', $hours) ? $isValid : false;
  $isValid = validateSelection('minute', $minutes) ? $isValid : false;
  $isValid = validateSelection('second', $seconds) ? $isValid : false;
 
  if ($isValid) {
    $time = mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']);
    $datetime = date('Y-m-d H:i:s', $time);
  } else {
    echo '<p class="error">Invalid selection.';
  }
}
 
// Output form
?>
<form method="post" action="#">
  <label>Date:</label>
  <select name="month"><?php echo buildOptionList($months); ?></select> / 
  <select name="day"><?php echo buildOptionList($days); ?></select> / 
  <select name="year"><?php echo buildOptionList($years); ?></select>
  <br />
 
  <label>Time:</label>
  <select name="hour"><?php echo buildOptionList($hours); ?></select> : 
  <select name="minute"><?php echo buildOptionList($hours); ?></select> : 
  <select name="second"><?php echo buildOptionList($hours); ?></select>
  <br />
 
  <button type="submit">Submit</button>
</form>
This is totally untested, but this should help you out. Read it and understand it, and you can implement it in your own form.
Post Reply