Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date_Add Function

Posted on 2011-03-16
7
Medium Priority
?
497 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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

886 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