Solved

Update DATE field for an existing record

Posted on 2014-07-30
4
271 Views
Last Modified: 2014-07-30
Upon reading a training item, users must click a button to acknowledge the training.  I have the form set up so that it passes a couple variables to a script that will add the DATE() to the record.  I cannot get the $date variable into the database.  Any advice would be greatly appreciated.

Here is my code for the page that inserts that date() into the existing record:
<?php

require_once '../scripts/app_config.php';
require_once '../scripts/database_connection.php';


$user_id = $_GET['user_id'];
$item_id = $_GET['item_id'];
$date = date();

$insert_sql = sprintf("INSERT INTO user_training_items (date_completed) VALUES ('%s') WHERE user_id=$user_id AND item_id=$item_id",
					  mysql_real_escape_string($date));
         
// Insert the user into the database
mysql_query($insert_sql);


// Redirect the user to the page that displays user information
header("Location: ../user_profile.php");
exit();
?>

Open in new window


The table in question (user_training_items) has three fields:
item_id
user_id
date_completed

The item_id and user_id are populated upon user account creation.  The date_completed field is NULL by default.
0
Comment
Question by:chaddcarr
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 200 total points
Comment Utility
If you are updating a record then you cannot INSERT

$insert_sql = sprintf("UPDATE user_training_items set date_completed = '%s' WHERE ...

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I get this error if I try to run the first part of your script:
Warning: date() expects at least 1 parameter, 0 given

Open in new window

You need to put at least one formatting parameter in there.  This is the recommended format for insert or update in MySQL.
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)

Open in new window


http://us1.php.net/manual/en/function.date.php
0
 

Author Closing Comment

by:chaddcarr
Comment Utility
Thanks!  That was it!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
date();
Almost certainly the wrong PHP function, or at least the arguments you need are omitted.  When you are not 100% certain about how a PHP function works, you can look up the PHP functions on the php.net web site.  Example:
http://php.net/manual/en/function.date.php
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

744 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

18 Experts available now in Live!

Get 1:1 Help Now