Solved

Update DATE field for an existing record

Posted on 2014-07-30
4
274 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
ID: 40230717
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 83

Expert Comment

by:Dave Baldwin
ID: 40230780
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
ID: 40230781
Thanks!  That was it!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40230784
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 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