9 Replies - 961 Views - Last Post: 10 July 2013 - 11:24 PM Rate Topic: -----

#1 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 09:55 PM

I am creating a database to add, edit and delete employees as a school project. The professor is allowing use to use Dreamweaver to assist in the creation of the project. I am also using mysql in wamp. We are required to use drop boxes, radio buttons, check boxes and text boxes to create the interface for the database. I have been able to create the table and add data to it, but when it comes to posting the date for the employee's hire date the table displays 0000-00-00 for the date. I have spent many hours searching for some information but was unable to find any help. Here is my code for the add and display files. Any help is greatly appreciated.

add file:

Line 48 is where I am running into issues.

<?php require_once('Connections/assign5.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}



$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO employees (first_name, last_name, birth_date, gender, hire_date,  manager, cur_employed) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['fName'], "text"),
                       GetSQLValueString($_POST['lName'], "text"),
                       GetSQLValueString($_POST['dateOfBirth'], "date"),
                       GetSQLValueString($_POST['gender'], "text"),
					   GetSQLValueString($_POST['year'], "int"."-".$_POST['month'], "int"."-".$_POST['day'], "int"),
                       GetSQLValueString(isset($_POST['manager']) ? "true" : "", "defined","'Yes'","'No'"),
                      GetSQLValueString($_POST['cur_employed'], "text"));

  mysql_select_db($database_assign5, $assign5);
  $Result1 = mysql_query($insertSQL, $assign5) or die(mysql_error());

  $insertGoTo = "dataBaseDisplay.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_assign5, $assign5);
$query_Recordset_Assign5 = "SELECT * FROM employees";
$Recordset_Assign5 = mysql_query($query_Recordset_Assign5, $assign5) or die(mysql_error());
$row_Recordset_Assign5 = mysql_fetch_assoc($Recordset_Assign5);
$totalRows_Recordset_Assign5 = mysql_num_rows($Recordset_Assign5);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>
</head>

<body>
<h2>Add Employee:</h2>
<p><a href="index.php">Admin Menu</a></p>
<form name="form" action="<?php echo $editFormAction; ?>" method="POST">
  <h3>First name:
  <input name="fName" type="text" />
    Last name: <input name="lName" type="text"  maxlength="16" />
    <br />
  </h3>
  <p></p>
  <h3>Date of Birth:
  <input name="dateOfBirth" type="text" maxlength="10" placeholder="YYYY-MM-DD" />
  </h3>
  <p>
    <label>
      <input type="radio" name="gender" value="M" id="gender_0" />
    Male</label>
    <br />
    <label>
      <input type="radio" name="gender" value="F" id="gender_1" />
    Female</label>
    <br />
  <h3>Hire Date</h3>
    <h3>Year: 
    <input name="year" type="text" placeholder="Year" maxlength="4" /> 
    Month: 
    <select name="month" id = "month">
      <option value = "1">January</option>
      <option value = "2">Febuary</option>
      <option value = "3">March</option>
      <option value = "4">April</option>
      <option value = "5">May</option>
      <option value = "6">June</option>
      <option value = "7">July</option>
      <option value = "8">August</option>
      <option value = "9">September</option>
      <option value = "10">October</option>
      <option value = "11">November</option>
      <option value = "12">December</option>
    </select> 
    Day: 
    <select name="day" id = "day">
      <option value = "1">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>
      <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>

  <br />
    </h3>
  <h3> Manager</h3>
Check if employee is a manager
  <input name="manager" type="checkbox" value="Manager" />
<br />
  <h3>Currently Employed</h3>
  <label>
      <input type="radio" name="cur_employed" value="Yes" id="cur_employed_0" />
      Yes</label>
    <br />
    <label>
      <input type="radio" name="cur_employed" value="No" id="cur_employed_1" />
      No</label>
    <br />
  </p>
  <br />
  <input name="addEmployee" type="submit" value="Add Employee" />
  <input type="hidden" name="MM_insert" value="form" />

</form>


<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset_Assign5);
?>



code form display file

<?php require_once('Connections/assign5.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_getEmployee = 10;
$pageNum_getEmployee = 0;
if (isset($_GET['pageNum_getEmployee'])) {
  $pageNum_getEmployee = $_GET['pageNum_getEmployee'];
}
$startRow_getEmployee = $pageNum_getEmployee * $maxRows_getEmployee;

mysql_select_db($database_assign5, $assign5);
$query_getEmployee = "SELECT * FROM employees ORDER BY emp_no ASC";
$query_limit_getEmployee = sprintf("%s LIMIT %d, %d", $query_getEmployee, $startRow_getEmployee, $maxRows_getEmployee);
$getEmployee = mysql_query($query_limit_getEmployee, $assign5) or die(mysql_error());
$row_getEmployee = mysql_fetch_assoc($getEmployee);

if (isset($_GET['totalRows_getEmployee'])) {
  $totalRows_getEmployee = $_GET['totalRows_getEmployee'];
} else {
  $all_getEmployee = mysql_query($query_getEmployee);
  $totalRows_getEmployee = mysql_num_rows($all_getEmployee);
}

$maxRows_getEmployee = 20;
$pageNum_getEmployee = 0;
if (isset($_GET['pageNum_getEmployee'])) {
  $pageNum_getEmployee = $_GET['pageNum_getEmployee'];
}
$startRow_getEmployee = $pageNum_getEmployee * $maxRows_getEmployee;

mysql_select_db($database_assign5, $assign5);
$query_getEmployee = "SELECT * FROM employees ORDER BY emp_no ASC";
$query_limit_getEmployee = sprintf("%s LIMIT %d, %d", $query_getEmployee, $startRow_getEmployee, $maxRows_getEmployee);
$getEmployee = mysql_query($query_limit_getEmployee, $assign5) or die(mysql_error());
$row_getEmployee = mysql_fetch_assoc($getEmployee);

if (isset($_GET['totalRows_getEmployee'])) {
  $totalRows_getEmployee = $_GET['totalRows_getEmployee'];
} else {
  $all_getEmployee = mysql_query($query_getEmployee);
  $totalRows_getEmployee = mysql_num_rows($all_getEmployee);
}
$totalPages_getEmployee = ceil($totalRows_getEmployee/$maxRows_getEmployee)-1;

$queryString_getEmployee = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getEmployee") == false && 
        stristr($param, "totalRows_getEmployee") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getEmployee = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getEmployee = sprintf("&totalRows_getEmployee=%d%s", $totalRows_getEmployee, $queryString_getEmployee);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


</form><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Table</title>
</head>

<body>
<h1>Current Employees </h1>
<table width="800" border="1">
  <tr>
    <th scope="col">ID Number</th>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">Birth Date</th>
    <th scope="col">Gender</th>
    <th scope="col">Hire Date</th>
    <th scope="col">Manager</th>
    <th scope="col">Currently Employed</th>
    <th scope="col">EDIT</th>
    <th scope="col">DELETE</th>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_getEmployee['emp_no']; ?></td>
      <td><?php echo $row_getEmployee['first_name']; ?></td>
      <td><?php echo $row_getEmployee['last_name']; ?></td>
      <td><?php echo $row_getEmployee['birth_date']; ?></td>
      <td><?php echo $row_getEmployee['gender']; ?></td>
      <td><?php echo $row_getEmployee['hire_date']; ?></td>
      <td><?php echo $row_getEmployee['manager']; ?></td>
      <td><?php echo $row_getEmployee['cur_employed']; ?></td><td align="center" valign="middle">edit</td>
      <td align="center">delete</td>
    </tr>
    <?php } while ($row_getEmployee = mysql_fetch_assoc($getEmployee)); ?>
</table>
<table border="0">
  <tr>
    <td><?php if ($pageNum_getEmployee > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_getEmployee=%d%s", $currentPage, 0, $queryString_getEmployee); ?>">First</a>
        <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_getEmployee > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_getEmployee=%d%s", $currentPage, max(0, $pageNum_getEmployee - 1), $queryString_getEmployee); ?>">Previous</a>
        <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_getEmployee < $totalPages_getEmployee) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_getEmployee=%d%s", $currentPage, min($totalPages_getEmployee, $pageNum_getEmployee + 1), $queryString_getEmployee); ?>">Next</a>
        <?php } // Show if not last page ?></td>
    <td><?php if ($pageNum_getEmployee < $totalPages_getEmployee) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_getEmployee=%d%s", $currentPage, $totalPages_getEmployee, $queryString_getEmployee); ?>">Last</a>
        <?php } // Show if not last page ?></td>
  </tr>
</table>
<p><a href="addEmployee.php"><strong>Add Employee</strong></a></p>
</body>
</html>
<?php
mysql_free_result($getEmployee);
?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP date concatenation from text and dropdown boxes

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 10:36 PM

your escaping of the date (line #48) is totally wrong. check what the escaping function expects as parameters and what you actually pass.
Was This Post Helpful? 1
  • +
  • -

#3 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 10:45 PM

I know very little about php. This is my second assignment dealing with php, so please bare with me. In the escaping of the date, is it because I am trying to pass a int instead of a string?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 10:54 PM

Quote

In the escaping of the date, is it because I am trying to pass a int instead of a string?

let’s examine the call in question a bit further:
// using line breaks to separate parameters
GetSQLValueString(
    $_POST['year'], 
    "int"."-".$_POST['month'], 
    "int"."-".$_POST['day'], 
    "int"
)

and the function call itself:
// using line breaks to separate parameters
function GetSQLValueString(
    $theValue, 
    $theType, 
    $theDefinedValue = "", 
    $theNotDefinedValue = ""
) 


first, let’s use some mock data for the $_POST and do any concatenation found in your call.
GetSQLValueString("2013", "int-07", "int-11", "int")


then we do a comparison against the function footprint:
$theValue           => "2013"
$theType            => "int-07"
$theDefinedValue    => "int-11"
$theNotDefinedValue => "int"



Now for the question, do these values together make sense?

This post has been edited by Dormilich: 10 July 2013 - 10:56 PM

Was This Post Helpful? 2
  • +
  • -

#5 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:01 PM

I see what you are getting at.
Was This Post Helpful? 0
  • +
  • -

#6 nandureddy  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 129
  • Joined: 31-January 11

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:03 PM

Try this:
GetSQLValueString($_POST['year'] ."-".$_POST['month'] ."-".$_POST['day'], "int").

Was This Post Helpful? 1
  • +
  • -

#7 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:12 PM

I got it to work using text instead of int. My database accepted it. Thank you for the guidance.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:16 PM

View Postnandureddy, on 11 July 2013 - 08:03 AM, said:

Try this:
GetSQLValueString($_POST['year'] ."-".$_POST['month'] ."-".$_POST['day'], "int").

that would return only the year.

Quote

I got it to work using text instead of int

and how do you prevent invalid values (e.g. like "YYYY-MM-DD")?
Was This Post Helpful? 0
  • +
  • -

#9 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:19 PM

To be honest, I don't know at this point. Do you have some direction?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: PHP date concatenation from text and dropdown boxes

Posted 10 July 2013 - 11:24 PM

if you canít parse the date string with strtotime(), chances are that itís not a date.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1