I am new to php and the part of my script that i am stuck on involves a date. On the page I want to chagne the date from 2009-02-16 to 02-16-09 (m-d-y). On the rest of my site I use the "echo date('m-d-y', strtotime($row_DetailRS1['end']))" and it has worked fine. However on this page I need to update the form and the user might change the date. If i use the strtotime function, it gets all jumbled up. I have spent 4 hours trying to solve this problem and am stuck.... Thanks in advance for any assistance, Ben <?php require_once('Connections/Morning.php'); ?><?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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_update"])) && ($_POST["MM_update"] == "form1")) { $updateSQL = sprintf("UPDATE data SET name=%s, title=%s, `start`=%s, `end`=%s, announcement=%s WHERE id=%s", GetSQLValueString($_POST['name'], "text"), GetSQLValueString($_POST['title'], "text"), GetSQLValueString($_POST['start'], "date"), GetSQLValueString($_POST['end'], "date"), GetSQLValueString($_POST['announcement'], "text"), GetSQLValueString($_POST['id'], "int")); mysql_select_db($database_Morning, $Morning); $Result1 = mysql_query($updateSQL, $Morning) or die(mysql_error()); $updateGoTo = "updatemaster.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } mysql_select_db($database_Morning, $Morning); $query_Recordset1 = "SELECT * FROM `data`"; $Recordset1 = mysql_query($query_Recordset1, $Morning) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); $maxRows_DetailRS1 = 10; $pageNum_DetailRS1 = 0; if (isset($_GET['pageNum_DetailRS1'])) { $pageNum_DetailRS1 = $_GET['pageNum_DetailRS1']; } $startRow_DetailRS1 = $pageNum_DetailRS1 * $maxRows_DetailRS1; mysql_select_db($database_Morning, $Morning); $recordID = $_GET['recordID']; $query_DetailRS1 = "SELECT * FROM `data` WHERE id = $recordID ORDER BY `start` DESC"; $query_limit_DetailRS1 = sprintf("%s LIMIT %d, %d", $query_DetailRS1, $startRow_DetailRS1, $maxRows_DetailRS1); $DetailRS1 = mysql_query($query_limit_DetailRS1, $Morning) or die(mysql_error()); $row_DetailRS1 = mysql_fetch_assoc($DetailRS1); if (isset($_GET['totalRows_DetailRS1'])) { $totalRows_DetailRS1 = $_GET['totalRows_DetailRS1']; } else { $all_DetailRS1 = mysql_query($query_DetailRS1); $totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1); } $totalPages_DetailRS1 = ceil($totalRows_DetailRS1/$maxRows_DetailRS1)-1; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Update Announcements</title> <link href="morning.css" rel="stylesheet" type="text/css"> </head> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="90" colspan="3"><div align="center"><table width="750" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="90" background="images/header_back.jpg"><div align="center" class="title"><? echo "$school" ; ?> </div></td> </tr> </table></div></td> </tr> <tr> <td width="14" height="25"> </td> <td background="images/menu_back_tmp.png" bgcolor="#FFFFFF"><table width="727" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="722" height="19" background="images/menu_back.png"> <span class="main"> <a href="add.php">Add</a> - <a href="updatemaster.php">Update</a> - <a href="deletemaster.php">Delete</a> - <a href="viewall.php">All Records</a> View Announcements For: <a href="today.php">Today</a> - <a href="tomorrow.php">Tomorrow</a> - <a href="monday.php">Monday</a> - <a href="future.php">Future</a></span></td> </tr> </table></td> <td width="15"> </td> </tr> <tr> <td width="14"> </td> <td width="727" height="400" align="center" valign="top" bgcolor="#FFFFFF"> <div align="center"> <br> <table width="699" border="0" align="center" cellpadding="0" cellspacing="5"> <tr> <td valign="top"><div align="left"> </div> <form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <br> <table width="95%" height="154" align="center"> <tr valign="baseline"> <td nowrap align="right">Id:</td> <td><div align="left"><?php echo $row_DetailRS1['id']; ?></div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Name:</td> <td><div align="left"> <input type="text" name="name" value="<?php echo $row_DetailRS1['name']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Title:</td> <td><div align="left"> <input type="text" name="title" value="<?php echo $row_DetailRS1['title']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Start:</td> <td><div align="left"> <input type="text" name="start" value="<?php echo $row_DetailRS1['start']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">End:</td> <td><div align="left"> <input type="text" name="end" value="<?php echo $row_DetailRS1['end']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td align="right" valign="top" wrap>Announcement:</td> <td><textarea name="announcement" cols="68" rows="15" wrap="VIRTUAL"><?php echo $row_DetailRS1['announcement']; ?></textarea></td> </tr> <tr valign="baseline"> <td nowrap align="right"><input type="submit" value="Update record"></td> <td> </td> </tr> </table> <input type="hidden" name="MM_update" value="form1"> <input type="hidden" name="id" value="<?php echo $row_DetailRS1['id']; ?>"> </form> <p> </p> <div align="left"> <p><br> <br> </p> </div></td> </tr> </table> <span class="main">Admin: <a href="login.php">Login</a></span><br> </div> </td> <td width="15"> </td> </tr> <tr> <td colspan="3"><table width="750" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="750" height="69" background="images/footer_back_tmp.png"><div align="center" class="footer"><span class="style4">Copyright ©2009</span> <a href="http://www.software4schools.com" target="_blank">Software 4 Schools</a> </div></td> </tr> </table></td> </tr> </table> </body> </html><?php mysql_free_result($Recordset1); mysql_free_result($DetailRS1); ?> PHP:
I'm not sure I understand your problem. Are you saying you need a way to force the user to post in a format like 2009-02-16?
Kind of... If you look at the two pages below, the update.php is what it looks like by default. Update2.php is what i want the user to see and be albe to modify the date. However if I submit with update2.php, the date gets all jumbled up. You can test out this site as it is just test data.... http://www.allstarcomputers.biz/morning/update.php?recordID=3 <br> http://www.allstarcomputers.biz/morning/update2.php?recordID=3 HTML:
$end = explode('/',$_POST['end']); $_POST['end'] = $end[1] . '/' . $end[0] . '/' . $end[2]; $start = explode('/',$_POST['start']); $_POST['start'] = $start[1] . '/' . $start[0] . '/' . $start[2]; PHP:
Thank you for the code. I just spent 45 minuets trying to figure out where to put the code and want needs to change. Do I remove this code and replace what you sent. Do i put your code at the top in the middle or what. Sorry like i said I am very new to php and just in the learning stage. GetSQLValueString($_POST['start'], "date"), GetSQLValueString($_POST['end'], "date"),
Don't remove anything, put it after the "if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {" line.
When i tried your code, it saves, the date but it saves it as 12/31/69 . I tried with and without the strtotime. I tried chaning the explod to - instead of / I even tried changing the order to $_POST['start'] = $start[1] . '-' . $start[2] . '-' . $start[0]; and i still cant get this thing to work.
Software4: You are trying to account for every possible way the user can enter a date. Valid or invalid. Why not go right to the source? Download or Code a simple JavaScript pop up calendar. Set the box to readonly so the USER must choose from the calendar. The calendar can be formatted to your spec. Seems like the cleanest solution from root.
<?php require_once('Connections/Morning.php'); ?><?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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_update"])) && ($_POST["MM_update"] == "form1")) { $updateSQL = sprintf("UPDATE data SET name=%s, title=%s, `start`=%s, `end`=%s, announcement=%s WHERE id=%s", GetSQLValueString($_POST['name'], "text"), GetSQLValueString($_POST['title'], "text"), GetSQLValueString($_POST['start'], "date"), GetSQLValueString($_POST['end'], "date"), GetSQLValueString($_POST['announcement'], "text"), GetSQLValueString($_POST['id'], "int")); mysql_select_db($database_Morning, $Morning); $Result1 = mysql_query($updateSQL, $Morning) or die(mysql_error()); $updateGoTo = "updatemaster.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } mysql_select_db($database_Morning, $Morning); $query_Recordset1 = "SELECT * FROM `data`"; $Recordset1 = mysql_query($query_Recordset1, $Morning) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); $maxRows_DetailRS1 = 10; $pageNum_DetailRS1 = 0; if (isset($_GET['pageNum_DetailRS1'])) { $pageNum_DetailRS1 = $_GET['pageNum_DetailRS1']; } $startRow_DetailRS1 = $pageNum_DetailRS1 * $maxRows_DetailRS1; mysql_select_db($database_Morning, $Morning); $recordID = $_GET['recordID']; $query_DetailRS1 = "SELECT * FROM `data` WHERE id = $recordID ORDER BY `start` DESC"; $query_limit_DetailRS1 = sprintf("%s LIMIT %d, %d", $query_DetailRS1, $startRow_DetailRS1, $maxRows_DetailRS1); $DetailRS1 = mysql_query($query_limit_DetailRS1, $Morning) or die(mysql_error()); $row_DetailRS1 = mysql_fetch_assoc($DetailRS1); if (isset($_GET['totalRows_DetailRS1'])) { $totalRows_DetailRS1 = $_GET['totalRows_DetailRS1']; } else { $all_DetailRS1 = mysql_query($query_DetailRS1); $totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1); } $totalPages_DetailRS1 = ceil($totalRows_DetailRS1/$maxRows_DetailRS1)-1; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Update Announcements</title> <link href="morning.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="datepickercontrol.js"></script> <link type="text/css" rel="stylesheet" href="datepickercontrol.css"> </head> <body> <input type="hidden" id="DPC_TODAY_TEXT" value="today"> <input type="hidden" id="DPC_BUTTON_TITLE" value="Open calendar..."> <input type="hidden" id="DPC_MONTH_NAMES" value="['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']"> <input type="hidden" id="DPC_DAY_NAMES" value="['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']"> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="90" colspan="3"><div align="center"><table width="750" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="90" background="images/header_back.jpg"><div align="center" class="title"><? echo "$school" ; ?> </div></td> </tr> </table></div></td> </tr> <tr> <td width="14" height="25"> </td> <td background="images/menu_back_tmp.png" bgcolor="#FFFFFF"><table width="727" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="722" height="19" background="images/menu_back.png"> <span class="main"> <a href="add.php">Add</a> - <a href="updatemaster.php">Update</a> - <a href="deletemaster.php">Delete</a> - <a href="viewall.php">All Records</a> View Announcements For: <a href="today.php">Today</a> - <a href="tomorrow.php">Tomorrow</a> - <a href="monday.php">Monday</a> - <a href="future.php">Future</a></span></td> </tr> </table></td> <td width="15"> </td> </tr> <tr> <td width="14"> </td> <td width="727" height="400" align="center" valign="top" bgcolor="#FFFFFF"> <div align="center"> <br> <table width="699" border="0" align="center" cellpadding="0" cellspacing="5"> <tr> <td valign="top"><div align="left"> </div> <form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <br> <table width="95%" height="154" align="center"> <tr valign="baseline"> <td nowrap align="right">Id:</td> <td><div align="left"><?php echo $row_DetailRS1['id']; ?></div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Name:</td> <td><div align="left"> <input type="text" name="name" value="<?php echo $row_DetailRS1['name']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Title:</td> <td><div align="left"> <input type="text" name="title" value="<?php echo $row_DetailRS1['title']; ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">Start:</td> <td><div align="left"> <input type="text" name="start" id="start" datepicker="true" datepicker_format="m/d/Y" value="<?php echo date('m-d-Y', strtotime($row_DetailRS1['start'])); ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td nowrap align="right">End:</td> <td><div align="left"> <input type="text" name="end" id="end" datepicker="true" datepicker_format="MM/DD/YYYY" value="<?php echo date('m-d-Y', strtotime($row_DetailRS1['end'])); ?>" size="32"> </div></td> </tr> <tr valign="baseline"> <td align="right" valign="top" wrap>Announcement:</td> <td><textarea name="announcement" cols="68" rows="15" wrap="VIRTUAL"><?php echo $row_DetailRS1['announcement']; ?></textarea></td> </tr> <tr valign="baseline"> <td nowrap align="right"><input type="submit" value="Update record"></td> <td> </td> </tr> </table> <input type="hidden" name="MM_update" value="form1"> <input type="hidden" name="id" value="<?php echo $row_DetailRS1['id']; ?>"> </form> <p> </p> <div align="left"> <p><br> <br> </p> </div></td> </tr> </table> <span class="main">Admin: <a href="login.php">Login</a></span><br> </div> </td> <td width="15"> </td> </tr> <tr> <td colspan="3"><table width="750" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="750" height="69" background="images/footer_back_tmp.png"><div align="center" class="footer"><span class="style4">Copyright ©2009</span> <a href="http://www.software4schools.com" target="_blank">Software 4 Schools</a> </div></td> </tr> </table></td> </tr> </table> </body> </html><?php mysql_free_result($Recordset1); mysql_free_result($DetailRS1); ?> PHP:
It's starting to seem like the problem might be with the database. After the "if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {" put an "echo $_POST['end'];" line, and see what date it outputs.
It displayed the date... and then the error below 02/19/2009 Warning: Cannot modify header information - headers already sent by (output started at /home/allstar/public_html/morning/updatedate.php:37) in /home/allstar/public_html/morning/updatedate.php on line 60
I also have this code that i use on another part of my program. I am trying to figure out how i can put this code and make it work on this page. $start=$_POST['start']; $start = date('Y/m/d', strtotime($start)); $end=$_POST['end']; $end = date('Y/m/d', strtotime($end));
A little closer... Here is the code that i have now $start=$_POST['start']; $start = date('Y-m-d', strtotime($start)); $end=$_POST['end']; $end = date('Y-m-d', strtotime($end)); //$updateSQL = sprintf("UPDATE data SET name=%s, title=%s, `start`=%s, `end`=%s, announcement=%s WHERE id=%s", $updateSQL = sprintf("UPDATE data SET 'name=%s', 'title=%s', '$start', '$end', 'announcement=%s' WHERE id=%s", GetSQLValueString($_POST['name'], "text"), GetSQLValueString($_POST['title'], "text"), //GetSQLValueString($_POST[$start], "date"), //GetSQLValueString($_POST[$end], "date"), GetSQLValueString($_POST['start'], "date"), GetSQLValueString($_POST['end'], "date"), GetSQLValueString($_POST['announcement'], "text"), GetSQLValueString($_POST['id'], "int")); PHP: and the error message is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''name='gdfgdf'', 'title='gdfgdfgfd'', '2009-02-18', '2009-02-26', 'announcement=' at line 1 Now the date is in the correct format. I noticed that the other fields say title= but the date just has the date...
Change the line to this: $updateSQL = sprintf("UPDATE data SET name=%s, title=%s, `start`='$start', `end`='$end', announcement=%s WHERE id=%s",