Ugluth Posted December 23, 2010 Share Posted December 23, 2010 Hello i have a table called movies on my database and i have a form where the user can input the data, press submit and add it to the database. My problem is that i only want the movie name to be a required field (the user doesn't set the id field, its auto incremented, so i leave that to the database). I'm using this query to insert the new movie to the database: $query = "INSERT INTO movies (movie_name, movie_category, release_date, movie_medium, rental_time, total_stock, current_stock, rented) VALUES ('$name', $category, '$release', $medium, $period, $total, $current, $rented)"; My problem is that this way all of the fields are required, else it generates an error: 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 ' '', , , , , )' at line 2 Which i understand why, but what can i do so only the movie name is required? All of the fields allow nulls so that is not the problem. I could check which variables have values and use a query with only the values that have a meaningfull value, but I don't think that is the best way to do it. That would have me create many different queries for each case, or use a gigantic query with some if's in it? (don't know if that would work, just an idea) Could someone please direct me to the correct way of doing this? Thank you in advance Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 if you're going to put the empty values into SQL, you'll need to single-quote the empty values. ... VALUES ('$name', $category, '$release', '$medium', '$period', '$total', '$current', '$rented')"; Quote Link to comment Share on other sites More sharing options...
Ugluth Posted December 24, 2010 Author Share Posted December 24, 2010 Thank you very much works fine that way for most of the fields, some fields though have are foreign key and i get a constraint violation when i try to enter the name only. The foreign keys are like this in the form: <td><label for="category">Category</label></td> <td><select name="category" id="category"> <option value="">Select a category</option> <?php while($row_cat = mysql_fetch_row($result_category)) { ?> <option value="<?php echo $row_cat[0]; ?>"><?php echo $row_cat[1]; ?></option> <?php } ?> </select> </td> I suppose the problem is with this line <option value="">Select a category</option> What should i put as a value on that so it doesn't require these? Thank you for your time. 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.