Jump to content

[SOLVED] Adding time into MySql


Democreous

Recommended Posts

I'm trying to add a time into a mySQL time table.  It's in the format 00:00:00, but it never is inserted into it.

The reason for this code is to sort through events by time.

 

the insertion code is as follows with variables $mm = minute, $ampm= am or pm, and $hh = hour

 

if ($ampm == pm) // Sets To a 24 hour format
{
$hh = ($hh+12);
}
$time=("$hh:$mm:00"); // Puts it into the MySql time format... or so I think
echo ("$time"); // I test to see if it diplays in the correct mysql format, which it does ex. 02:30:00

$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) 
{ 
  // Since the entire script depends on connection, die if connection fails 
  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($T, $con); 

$sql = "UPDATE calendar_events SET event_title='$title',event_desc='$description',event_date='$idate',event_time='$time' WHERE event_id='$id'"; 
         $result = mysql_query($sql); 
	 echo "<div class=\"ttl\">Event Updated, <a href=\"calendar.php\">Return to main page</a></div>";

}

Link to comment
Share on other sites

ugg.  I accidently posted the edit script, the add script is suppose to be

 

	// SET TIME ACCURATE
if ($ampm == pm)
{
$hh = ($hh+12);
}
$time=("$hh:$mm:00");

echo ($time);

// Connect to MySQL database 
$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) 
{ 
  // Since the entire script depends on connection, die if connection fails 
  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($T, $con); 

$query = "INSERT INTO calendar_events(event_title, event_desc, event_date, event_time)
VALUES('".$_POST['title']."','".$_POST['desc']."','".$_POST['idate']."','".$_POST['time']."')";
mysql_query($query);

  echo "<div class=\"ttl\">Event Added <a href=\"calendar.php\">Return to main page</a></div>";

  mysql_close($con);

Link to comment
Share on other sites

Have you though about the timestamp data type.

 

If you are simply inserting the current time into the database, then you can set the field as a timestamp, and make it insert the current stamp automatically.

 

If you are inserting future, or past dates, then this will not work so well.

Link to comment
Share on other sites

echo your query and see if the time is being passed into it properly.

 

If so, try copying and pasting the echo'd query into phpmyadmin and see if it gives you errors.

 

 

and also, you should sanitize this query before inserting it. It is not good practice to insert directly from the $_POST fields as mysql injection attack could be possible.

 

You should pass your $_POST vars through mysql_real_escape_string() before sending to the query.

 

$query = "INSERT INTO calendar_events(event_title, event_desc, event_date, event_time)

VALUES('".$_POST['title']."','".$_POST['desc']."','".$_POST['idate']."','".$_POST['time']."')";

mysql_query($query);

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.