billy_111 Posted June 9, 2010 Share Posted June 9, 2010 Hey, I am trying to do an insert statement where i am trying to add a "Recurring weeks" method checking to see the value of a field (1, 2, 3.. etc) and insert multiple times with an increase of the date by 7 days depending on the number of recurring weeks. For example, if you look at this image: http://www.glofamily.com/glo/images/courses.jpg You can see that a form where users can insert a course. Also look at the "Recurring weeks" dropdown. Taking this image as an example, i would have 3 insert statements, the first would have the date of 03/06/2010 and then i want to add another 2 with dates of 10/06/2010, 17/06/2010.. I thought i would need a for loop and have managed to put something together, but i don't think it's the right way to do it.. public function insertCourse($category){ $date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'] ." ".$_POST['hours'].":".$_POST['minutes'].":00"; $startdate = "0000-00-00 ".$_POST['starthours'].":".$_POST['startminutes'].":00"; $enddate = "0000-00-00 ".$_POST['finishhours'].":".$_POST['finishminutes'].":00"; $recWeeks = mysql_real_escape_string($_POST['recWeeks']); for($i=0; $i<recWeeks; $i++){ $sql = "INSERT INTO tbl_courses (catID, locID, date, startdate, enddate, title, body, email, practitioner, phone, image, date_added) VALUES ( '".mysql_real_escape_string($_POST['catID'])."', '".mysql_real_escape_string($_POST['locID'])."', '".mysql_real_escape_string($date)."', '".mysql_real_escape_string($startdate)."', '".mysql_real_escape_string($enddate)."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['body'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['practitioner'])."', '".mysql_real_escape_string($_POST['phone'])."', '".mysql_real_escape_string($_FILES['image']['name'])."', now() )"; $result = mysql_query($sql) or die(mysql_error()); } return "Successfully added course"; } But don't know where to go from here, i would probably need to add .mysql_real_escape_string($date)." + INTERVAL \'14\' DAY But how can i incorporate this in my code?? Can someone please help? Thanks again Billy Link to comment https://forums.phpfreaks.com/topic/204276-insert-date-7-days/ Share on other sites More sharing options...
JonnoTheDev Posted June 9, 2010 Share Posted June 9, 2010 Look at the strtotime function. http://uk2.php.net/strtotime Example <?php $date = '2010-06-09'; $next = strtotime("+7 day",strtotime($date)); $next = date("Y-m-d", $next); print $next; ?> Link to comment https://forums.phpfreaks.com/topic/204276-insert-date-7-days/#findComment-1069889 Share on other sites More sharing options...
billy_111 Posted June 9, 2010 Author Share Posted June 9, 2010 Hey, I managed to get it working like so: $recWeeks = mysql_real_escape_string($_POST['recWeeks']); for($i=0; $i<$recWeeks; $i++){ $sql = "INSERT INTO tbl_courses (catID, locID, weeks, date, startdate, enddate, title, body, email, practitioner, phone, image, date_added) VALUES ( '".mysql_real_escape_string($_POST['catID'])."', '".mysql_real_escape_string($_POST['locID'])."', '".mysql_real_escape_string($_POST['recWeeks'])."', '".mysql_real_escape_string($date)."' + INTERVAL ($i * 7) DAY, '".mysql_real_escape_string($startdate)."', '".mysql_real_escape_string($enddate)."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['body'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['practitioner'])."', '".mysql_real_escape_string($_POST['phone'])."', '".mysql_real_escape_string($_FILES['image']['name'])."', now() )"; $result = mysql_query($sql) or die(mysql_error()); } However, i can see a problem when it comes to updating. This is my current update function: public function updateCourse($category){ if(is_numeric($_POST['ID'])){ if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != ""){ $image = ", image = '".mysql_real_escape_string($_FILES['image']['name'])."'"; }else{ $image = ''; } $date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'] ." ".$_POST['hours'].":".$_POST['minutes'].":00"; $startdate = "0000-00-00 ".$_POST['starthours'].":".$_POST['startminutes'].":00"; $enddate = "0000-00-00 ".$_POST['endhours'].":".$_POST['endminutes'].":00"; $sql = "UPDATE tbl_courses SET date = '".mysql_real_escape_string($date)."', startdate = '".mysql_real_escape_string($startdate)."', enddate = '".mysql_real_escape_string($enddate)."', catID = '".mysql_real_escape_string($_POST['catID'])."', locID = '".mysql_real_escape_string($_POST['locID'])."', title = '".mysql_real_escape_string($_POST['title'])."', body = '".mysql_real_escape_string($_POST['body'])."', email = '".mysql_real_escape_string($_POST['email'])."', practitioner = '".mysql_real_escape_string($_POST['practitioner'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', date_added = now() ".$image." WHERE ID = ".$_POST['ID'].""; $result = mysql_query($sql); return $result; }else{ die('ID needs to be numeric'); } } How can i use the same method do update the number of recurring weeks? Thanks Link to comment https://forums.phpfreaks.com/topic/204276-insert-date-7-days/#findComment-1069904 Share on other sites More sharing options...
JonnoTheDev Posted June 9, 2010 Share Posted June 9, 2010 UPDATE table SET date=date+INTERVAL 7 DAY Use the current date value Link to comment https://forums.phpfreaks.com/topic/204276-insert-date-7-days/#findComment-1069912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.