Solved

Update DATE field for an existing record

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

867 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

17 Experts available now in Live!

Get 1:1 Help Now