zb4885 Posted April 3, 2017 Share Posted April 3, 2017 I am a student currently learning PHP and SQL. We are building a PHP form and the input values need to be inserted into a MySQL database. (I am using PhpMyAdmin.) When I load the page, I just see this error, and my teacher said he is not familiar with how to fix it! "Database update failed.Incorrect date value: 'dateOfRelease' for column 'ReleaseDate' at row 1" The name of the column in my database that I am trying to insert the date into is called ReleaseDate, with a data type of 'date'. I am using the html input type 'date' in my form. The input field is named 'dateOfRelease'. I have uploaded the code I have so far, I hope someone can help. Thanks, Zara addMovieForm.php Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 3, 2017 Share Posted April 3, 2017 You're trying to insert the literal string “dateOfRelease” as a date, which of course makes no sense. I assume you meant to insert $_POST['dateOfRelease'], but your code is all over the place. You start with an INSERT query referencing data that doesn't even exist at this point, then you do a bit of form validation, and then you do nothing (when the query should happen). Could it be that you've blindly copied and pasted this stuff without really understanding the purpose? Quote Link to comment Share on other sites More sharing options...
zb4885 Posted April 3, 2017 Author Share Posted April 3, 2017 Hi, Thanks for your response. I have followed quite a few different tutorials on YouTube, and people seem to have different ways of doing things, so yes.. that's probably why my code is all over the place. Sorry, I am very new to this! Can you suggest how to fix it? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 3, 2017 Share Posted April 3, 2017 Programming is really all about understanding the problem and not so much about producing code. So start with a basic, informal concept of what happens when; write it down if necessary. Then create a script with the basic program structure. Leave out the details and use TODO comments instead. And when that's done, you can start writing the actual code. The concept is pretty simple in your case: Did we receive a POST request? Yes: Validate the input, insert the data if everything is correct, then do a redirect No: Display the form This leads to the following basic structure: <?php // TODO: establish database connection if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = []; // TODO: validate POST parameters, collect errors in $errors array if (!$errors) { // TODO: insert data, then redirect the user } else { // TODO: display errors } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <!-- the form goes here --> </body> </html> Now you can fill in the blanks. Quote Link to comment Share on other sites More sharing options...
zb4885 Posted April 3, 2017 Author Share Posted April 3, 2017 Thanks for explaining Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 3, 2017 Share Posted April 3, 2017 also, since you are using a mysql database, this thread is in the wrong forum section and may not even be looked at by those that would help with it. after you get the program structured correctly, you should start small, with a single form field and the form processing code for it. then, to add the other forms fields, rather than to type or copy/paste code for each possible form field (the point of programming isn't to see how much code you can produce, but to produce the code efficiently), you need to dynamically process the form data, by operating on it as a set. arrays are for sets of data. in the example code that Jacques1 has shown, he used an array to hold the errors. this replaces the long list of error variables you defined in your code and an array will work with any number of errors. a single statement is all that is required to define and initialize the array variable. to dynamically process the form data, in it's simplest form, you would make an array that lists the form fields (as the array keys) and a label/display-name for each field (as a sub-array for each field). you could later enhance this method by adding other defining information to the sub-array to control what the code does, such as add specialized validation rules. this array would be looped over to supply the form field names so that you can validate the 'required' form fields. the label/display-name would be used when dynamically producing the error message. this will eliminate most of the validation logic you have now, replacing it with a simple loop. next, you need to define what the submitted data means and what you are going to do with it. you are primarily submitting information about a movie. the unique one-time information would be stored directly in the movie table. multiple values, such as stars in the movie or ratings, would be stored in a separate table for each purpose, and related back to the correct movie using the movie id. the genre, director, producer, distributor, star, and nationally tables would be where defining information is stored. you would store new selections into these tables, but the value you enter into other tables would be the id from these defining tables. you would dynamically produce select/option menus from the data in these defining tables. the nationality of the director and producer would be stored in the director and producer tables using the nationality id, from the nationally table, and would be selected when the director or producer information is created. when the movie information is entered, you would only be selecting from among existing genre, director, producer, distributor, and star information and it would be the id's of the selection that you store in the movie (and possibly stars in the movie and rating) table. yes, you can enter new information for the defining tables at the same time you enter the movie information, but you would need to insert the new information/detecting and handling duplicates, then get the last insert id and use it in the movie insert query. this logic is probably beyond your ability at this point. you should also use the php PDO extension to interface with the msyql database server (it is much easier to use than the php mysqli extension), use prepared queries to supply data values to the sql statement (this will eliminate the need to have logic in your code to escape data being put into the sql query statement and will insure that sql injection cannot occur), and use exceptions to handle database connection and statement errors (this will eliminate the need to have logic at each database statement that can fail.) doing these things will eliminate all the repetition in your code, greatly simplifying it, so that you can concentrate on writing the program logic to accomplish the task, rather than on writing a wall of code dealing with the implementation details. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 3, 2017 Share Posted April 3, 2017 also, since you are using a mysql database, this thread is in the wrong forum section and may not even be looked at by those that would help with it.Moved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.