Jump to content

datetime fields not updating in db


peppericious

Recommended Posts

I have a page where "Events" which may be of interest to site visitors can be recorded.

 

Among other info, an "Event" has a "from" date and a "to" date (to show that an event will run from October 29th to October 31st, for example).

 

All event info is being written to the db, but the "from" and "to" dates are not. (In the db, "0000-00-00 00:00:00" is being entered in the "from" and "to" columns.)

 

In the Events table, both event_date and event_end_date are of type datetime. Dates are entered into the form fields via a javascript datepicker and are in the format "24/09/2011".

 

I'm using this code:

// Check for a Start Date:
	if (empty($_POST['from'])) {
		$errors[] = 'You forgot to enter a Start Date for the event.';
	} else {
		$from = $_POST['from'];
	}

	// Check for an End Date:
	if (empty($_POST['to'])) {
		$errors[] = 'You forgot to enter an End Date for the event.';
	} else {
		$to = $_POST['to'];
	}

 

The db query is this:

if (empty($errors)) { // If everything's OK.
// Insert the Event info in the database...
	// Make the query:
	$q = "INSERT INTO
			events (title, event_date, event_end_date, venue, ext_link, blurb)
			VALUES ('$title', '$from', '$to', '$venue', '$ext_link', '$blurb')
			";		
	$r = @mysqli_query ($dbc, $q); // Run the query.
	if ($r) { // If it ran OK.
		// Print a message:
		echo "<h2>Event info added!</h2>
		<h3>Your entry is now visible on the <a href='index.php'>Home Page</a>.</h3>
		<p><a href='logout.php'><strong>Log out</strong></a></p>
		"
		;						
	mysqli_close($dbc);		 
include('inc/footer.php');
exit();	

 

And the form is this:

<form id="form1" name="form1" method="post" action="add_event.php">
	<label for="title">Title</label>
	<input type="text" name="title" id="title" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" />
	<script>
		$(function() {
			var dates = $( "#from, #to" ).datepicker({
				defaultDate: "",
				changeMonth: true,
				numberOfMonths: 1,
				onSelect: function( selectedDate ) {
					var option = this.id == "from" ? "minDate" : "maxDate",
						instance = $( this ).data( "datepicker" ),
						date = $.datepicker.parseDate(
							instance.settings.dateFormat ||
							$.datepicker._defaults.dateFormat,
							selectedDate, instance.settings );
					dates.not( this ).datepicker( "option", option, date );
				}
			});
		});
	</script>
	<label for="from" >*From</label>
	<input type="text" name="from" id="from" value="<?php if (isset($_POST['from'])) echo $_POST['from']; ?>" />

	<label for="to" ">*To</label>
	<input type="text" name="to" id="to" value="<?php if (isset($_POST['to'])) echo $_POST['to']; ?>" />
  	
	<label for="venue">*Venue</label>
	<input type="text" name="venue" id="venue" value="<?php if (isset($_POST['venue'])) echo $_POST['venue']; ?>" />
  
	<label for="blurb">*Description</label>
	<textarea name="blurb" id="blurb" value="<?php if (isset($_POST['blurb'])) echo $_POST['blurb']; ?>" rows="5" ></textarea>
  
	<label for="ext_link">External link (optional, entered like "www.example.com")</label>
	<input type="text" name="ext_link" id="ext_link" value="<?php if (isset($_POST['ext_link'])) echo $_POST['ext_link']; ?>" />
  
	<input type="submit" name="submit" value="Insert Event Info" class="submit" /></p>
	<input type="hidden" name="submitted" value="TRUE" />
</form>

 

Can anyone tell me why the dates aren't being recorded? Any help will be greatly appreciated.

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.