Solved

Date_Add Function

Posted on 2011-03-16
7
488 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
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

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

Author Comment

by:rromanjr
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:shinuq
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now