crentzc
crentzc

Reputation: 23

I want to add 30 days to mysql date row

My Code.

    <?php //data.php
require_once 'db.php'; 

// Get values from form
$Fname       = $_POST['first_name'];
$Lname       = $_POST['last_name']; 
$web         = $_POST['web'];   
$email       = $_POST['email'];
$date        = $_POST['date'];
// Insert data into mysql
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql); 
$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
header('Location: ../index.php');
}
else {
echo "ERROR";
}

// close mysql
mysql_close();
?> 

My problem, i want to add those 30 days to the date column in database when form execute`s this file and inserts fname in fname, web in web, and date in date + 30 interval.

Thank you.

$sql="INSERT INTO users (first_name, last_name, web, email, date)
    VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
    $result = mysql_query($sql); 
    ***$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)"; 
    $result = mysql_query($sql);***

Upvotes: 1

Views: 2413

Answers (4)

Kamal AL-Hinai
Kamal AL-Hinai

Reputation: 104

Use the following query :

 $sql="INSERT INTO users (first_name, last_name, web, email, date)
    VALUES ('$Fname', '$Lname', '$web', '$email',DATE_ADD('$date', INTERVAL 30 day)  , NOW())";

Upvotes: 1

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

Here is the part you need to change

$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW() + INTERVAL 30 DAY)";

Upvotes: 0

Anand Solanki
Anand Solanki

Reputation: 3425

Try this:

    $sql="INSERT INTO users (first_name, last_name, web, email, date)
    VALUES ('$Fname', '$Lname', '$web', '$email', '$date', DATE_ADD(NOW(), INTERVAL 30 DAY))";

    $result = mysql_query($sql); 

>> you are adding date after query executes, but do the same when query executes.
  • Thanks

Upvotes: 0

Do like this...

$sql="INSERT INTO `users` (`first_name`, `last_name`, `web`, `email`, `date`)
    VALUES ('$Fname', '$Lname', '$web', '$email', DATE_ADD('$date', INTERVAL 30 DAY))";

Upvotes: 1

Related Questions