Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In a previous video, I described how to use the Record Insertion Wizard to create a data entry form that would collect information from the user and add it to the MySQL database, but I showed that there is a challenge with working with dates. That's because the code that's created by Dreamweaver doesn't do an adequate job of formatting dates as they're expected by MySQL. I'm working in the file, insertfromwizard2.php, in the root folder of the current site. Here is the problem code.
It starts with a case statement, and it's within a function named GetSQLValueString. The case statement for dates assumes that the user has typed in a value in a format that's acceptable to MySQL. Clearly, that isn't always the case. So I'm going to replace this string with a little bit more complex PHP code that uses the Zend_Date class that I've demonstrated in previous videos. I'll go to the top of the file and add a require_once command to incorporate the Zend_Date class into my page.
I'll add a PHP code block and then the require_once command and then the Zend/Date.php file. Make sure you complete the PHP code block at this point. Then go down to the offending code. Here is the line of code I need to replace. I'm going to select and delete it, and then I'll replace it with a complete if else statement. I'll start by creating the entire conditional logic, with if and a pair of parentheses, and a pair of braces, and then an else, and another pair of braces.
Then I'll go back to the parentheses after the if and add the condition I'm examining. I'm asking the question, did the user type in a value at all? I'll enter $theValue and then the equality operator, double equals, and then quote, quote, meaning blank string, Dreamweaver's syntax checking now shows me that there are no syntax errors. I now have a complete conditional block. If the value that the user typed in is blank, I'm going to set it to a value of null, using a very simple assignment statement of theValue =, and then the word "NULL" in uppercase.
In the else clause, I'm going to assume that the user typed in a value formatted as a date, in the format month/day/year, and eventually I'll add validation rules into my data entry form that ensures the value is typed in in this format. I'll take the value the user typed in and turn it into an instance of the Zend_Date class. I'll create a variable called zendDate, and I'll create it from a new instance of the Zend_Date class, and the first value that I'll pass in is the value that the user typed in in the form, theValue.
The second part is the format that I'm expecting. I'll type that in as a literal string, using quote, an uppercase M for the Month, then a slash, then a lowercase d for the date as a number, then a slash, and then four ys for the year. I now have an instance of the Zend_Date class, representing the value the user typed in. I can format it any way I want. I'll create a new line of code, and I'll replace the variable of theValue using a single quote, a formatted date, and another single quote.
I'll start with a single quote wrapped in double quotes, then a period, indicating that I'm going to concatenate, or append another value. Then I'll call the zendDate objects to string method. I'll pass in a formatting string, indicating the formatting that's expected by MySQL. In double quotes, it will look like this: yyyy for a four-digit year, then a dash, then MM in uppercase, then a dash, then dd in lowercase for the day, and finally, I'll concatenate another single quote at the end.
So now I have a formatted date string wrapped in single quotes. That's the extent of the changes to my data entry form's code. I'll save my changes and then run the form in the browser. I'll type in some values. The FirstName will be Mikey, then Jones. Make sure that you type in a valid date of birth in the common format used in the United States. I'll type in a value of 5/12/1963, type in any e-mail address, and then type in any other values you like in the rest of the form.
Because of certain restrictions in the database structure, you must type in values into all of these form controls. Once you've typed in all of the values, click Insert record. You should go directly to the explorerlist page and see that this time the date was entered correctly. Here is my new record, and the date is displayed, formatted by the Zend_Date class. So that's a look at how to customize your data entry forms after Dreamweaver has generated the code.
This strategy can be used both for code that's generated by the Record Insertion Wizard and for code that's added to existing forms, which I'll show you how to build in another video.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 80359 Viewers
80 Video lessons · 132730 Viewers
52 Video lessons · 66269 Viewers
59 Video lessons · 52047 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.