Jump to content

Insert date +7 days


billy_111

Recommended Posts

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
Share on other sites

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
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.