Solved

Update DATE field for an existing record

Posted on 2014-07-30
4
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

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.

Question has a verified solution.

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

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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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