Jump to content

Insert data to a table


Ugluth

Recommended Posts

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.