Solved

Date_Add Function

Posted on 2011-03-16
7
494 Views
Last Modified: 2012-08-13
I am having a problem with DATE_ADD(NOW(), INTERVAL 2 HOUR) within php adding a record to mysql database.  The statement works if I use it right in mysql workbench but not in data-entry.php page.  All I get is 00-00-00 00:00:00 when i display the record if I added it through the php page.  There is no mysql error nor php error

It works correctly when I use it within my update_entry.php page.  

Here are a couple of my sql statements I use in my php pages.

This is the statement on my record entry page that DOES NOT WORK
               $sql = "INSERT INTO NetDevice (date_time, serialnum, modelnum, idManu, idDevType, idLocation, IPAdd, mask, MAC, UnitName, username, password, firmware, idResort) VALUES (DATE_ADD(NOW(), INTERVAL 3 HOUR), '".$_POST['SN']."', '".$_POST['ModelNum']."', '".$_POST['Manu']."', '".$_POST['DType']."', '".$_POST['Location']."', '".$_POST['IPAdd']."', '".$_POST['mask']."', '".$_POST['MAC']."', '".$_POST['UnitName']."', '".$_POST['username']."', '".$_POST['Password']."', '".$_POST['FW']."', '".$_POST['Resort']."')";      

This is the statement in my update entry php page that DOES WORK.

        $sql = "UPDATE NetDevice SET date_time=DATE_ADD(NOW(), INTERVAL 3 HOUR), serialnum='".$_POST['SN']."', modelnum='".$_POST['ModelNum']."', idManu='".$_POST['Manu']."', idDevType='".$_POST['DType']."', idLocation='".$_POST['Location']."', IPAdd='".$_POST['IPAdd']."', mask='".$_POST['mask']."', MAC='".$_POST['MAC']."', UnitName='".$_POST['UnitName']."', username='".$_POST['username']."', password='".$_POST['Password']."', firmware='".$_POST['FW']."', idResort='".$_POST['Resort']."' WHERE IPAdd='".$_POST['IPAdd']."'";                                               

I can also type this statement into mysql workbench and get the data to insert correctly.  I did not use all the values, only the not_null columns.

INSERT INTO NetDevice (date_time, serialnum, modelnum, IPAdd, mask, MAC) VALUES (DATE_ADD(NOW(), INTERVAL 3 HOUR), 'serialnum1', 'modelnum1', 'IPadd1', 'mask1', 'mac2');      
 
0
Comment
Question by:rromanjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:nanharbison
ID: 35153274
Have you getting mysql errors using
" or die (mysql_error());"
0
 

Author Comment

by:rromanjr
ID: 35153318
No...I have no errors using the "or die (mysql_error())"

here is my expanded code including the or die statement:

include("../scripts/dbconn.php");
if (mysqli_connect_errno())
      {
        printf("Connection Failed: %s\n", mysqli_connect_error());
        exit();
      }
else
      {
        //add data to net device table
        $sql = "UPDATE NetDevice SET date_time=DATE_ADD(NOW(), INTERVAL 2 HOUR), serialnum='".$_POST['SN']."', modelnum='".$_POST['ModelNum']."', idManu='".$_POST['Manu']."', idDevType='".$_POST['DType']."', idLocation='".$_POST['Location']."', IPAdd='".$_POST['IPAdd']."', mask='".$_POST['mask']."', MAC='".$_POST['MAC']."', UnitName='".$_POST['UnitName']."', username='".$_POST['username']."', password='".$_POST['Password']."', firmware='".$_POST['FW']."', idResort='".$_POST['Resort']."' WHERE IPAdd='".$_POST['IPAdd']."'";                                               
        //Run mysqli query
        $res = mysqli_query($dbconn, $sql) or die(mysqli_error($dbconn));
        
echo "Record Updated Successfully!";
      }
mysqli_close($dbconn);

0
 

Author Comment

by:rromanjr
ID: 35153324
Sorry I added the update php code that works....BELOW is the insert php code that does not work


include("../scripts/dbconn.php");
//test for successful connection to database

if (mysqli_connect_errno())
      {
        printf("Connection Failed: %s\n", mysqli_connect_error());
        exit();
      }
else
      {
        //add data to net device table
        $sql = "INSERT INTO NetDevice (date_time, serialnum, modelnum, idManu, idDevType, idLocation, IPAdd, mask, MAC, UnitName, username, password, firmware, idResort) VALUES (DATE_ADD(NOW(), INTERVAL 2 HOUR), '".$_POST['SN']."', '".$_POST['ModelNum']."', '".$_POST['Manu']."', '".$_POST['DType']."', '".$_POST['Location']."', '".$_POST['IPAdd']."', '".$_POST['mask']."', '".$_POST['MAC']."', '".$_POST['UnitName']."', '".$_POST['username']."', '".$_POST['Password']."', '".$_POST['FW']."', '".$_POST['Resort']."')";      
        //Run mysqli query
        $res = mysqli_query($dbconn, $sql) or die(mysqli_error($dbconn));
        
echo "Record Added Successfully!";
      }
mysqli_close($dbconn);
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 35154418
Please define the field names in quotes like

INSERT INTO NetDevice (`date_time`, `serialnum`, `modelnum`.............

This may be the problem with

Hope this helps
0
 
LVL 17

Accepted Solution

by:
nanharbison earned 500 total points
ID: 35157675
So it must be some picky detail, because you are calling it correctly.
This might be grasping at straws, but did you try setting the date time equal to a variable first? SOmetimes that works for me.
$thistime = (DATE_ADD(NOW(), INTERVAL 2 HOUR);

for the insert query:
'".$thistime."'

Open in new window

0
 

Author Comment

by:rromanjr
ID: 35158304
I set the variable and it worked.  Not sure why it would't work directly in the sql statement.  For now the variable method will work....thanks for the thought nanharbison.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 35158450
I have had issues like this happen to me often enough that I am good at making little changes and trying them, but I am not an expert on why you can set a table field equal to the date-time thing and then you CAN'T include it in other ways!
Glad to help!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question