Solved

Update DATE field for an existing record

Posted on 2014-07-30
4
273 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

770 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