This topic is locked

Update Date field on edit page

8/29/2007 5:05:18 PM
PHPRunner General questions
C
Crimson author

Hello,
I was wondering if anyone could tell me how to show the current date and time in a date field when I click to edit a record?
I have a date field I need to update to the current time and date. On the add page it is easy, I just put date("Y-m-d g:i:s a") in the default field and the current date and time appear as soon as the page loads. User never has to input anything. However, if I click the edit link to edit a record, it shows the time and date already in the database. How do I tell it to blank out that field and insert he current time/date? Seems like there should be a way to do it in the Events screen, but I am not sure what to put where. Is there a way?

A
alang 8/29/2007

Try adding the following code to Edit Page - Before Display event:
$smarty->assign("value_<your field name here>",now());

C
Crimson author 9/6/2007

Try adding the following code to Edit Page - Before Display event:

$smarty->assign("value_<your field name here>",now());


You are awesome. Thanks. Only problem I had with that one line of code was it was showing the time in 24 hour format. I needed 12 hour. I replaced the now() with date("Y-m-d g:i:s a") and it works beautifully.
Again thanks.

L
laonian 10/9/2007

Try adding the following code to Edit Page - Before Display event:

$smarty->assign("value_<your field name here>",now());



Sorry to pick up an old thread. I tried your code and did not see an update in my date field. All old values stay unchanged.

A
alang 10/9/2007

Sorry to pick up an old thread. I tried your code and did not see an update in my date field. All old values stay unchanged.


Can you post the code you tried please.

L
laonian 10/10/2007

Can you post the code you tried please.


Sorry. I tried it again and it worked perfectly. I did not remove "<" and ">" symbols in earlier attempts. I have zero background in programming. Here I have got a question on this one. I want to assign the value now() to a date field only when its pre-exsiting value is a fixed date, e.g. 2007-01-01. I have got something (from the support) like:

[codebox]global $conn,$strTableName,$strWhereClause;

$str = "select * from ".$strTableName." where ".$strWhereClause;

$rs = db_query($str,$conn);

if ($data["DateField"]=="2007-01-01")

$smarty->assign("value_Purchase_date",now());

[/codebox]
I changed the TableName with my table name, and Datefield to my table's date fiield. When I run it, I got a syntax error. Could you help me with the code? Thanks.

A
alang 10/10/2007

It looks like you are missing a line of code. Try:
[codebox]global $conn,$strTableName,$strWhereClause;

$str = "select * from ".$strTableName." where ".$strWhereClause;

$rs = db_query($str,$conn);

$data = db_fetch_array($rs); // <<< missing this line to return first record of query result

if ($data["DateField"]=="2007-01-01")

$smarty->assign("value_Purchase_date",now());

[/codebox]

L
laonian 10/10/2007

It looks like you are missing a line of code. Try:

...



AlanG,
I added the missing line, but still have Syntax errors after clicking "EDIT". I have a table named jolab_ordering, with fields `ID`, `Date`, `Name`, `Item`, `Company`, `Order_NO`, `Onlin_Link`, `Unit`, `Unit_Price`, `Quantity` in the _add.php page and fields `Purchase_Date`, `Final_Price`, `Comfirmation_NO`, `Status`, `Delivery_Date`, `Receiver`, `Comments` in the _edit.php page, and 'References' in none of them. Following is my actual code:

[codebox]//** Custom code ****

// put your custom code here
global $conn,$strjolab_ordering,$strWhereClause;

$str = "select Purchase_Date, Final_Price,

Comfirmation_NO, Status, Delivery_Date,

Receiver, Comments from ".$strjolab_ordering." where ".$strWhereClause;

$rs = db_query($str,$conn);

$data = db_fetch_array($rs); // <<< missing this line to return first record of query result

if ($data["Purchase_Date"]=="2007-01-01")

$smarty->assign("value_Purchase_Date",now());[/codebox]
****The floowing is the error message****

Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where `jolab_ordering`.`ID`=117' at line 3

URLlocalhost/PHPRunner/jolab_ordering_edit.php?editid1=117

Error file C:\Program Files\PHPRunner\projects\orderpage\output\include\dbconnection.php

Error line 26

SQL queryselect `ID`, `Date`, `Name`, `Item`, `Company`, `Order_NO`, `Onlin_Link`, `Unit`, `Unit_Price`, `Quantity`, `Purchase_Date`, `Final_Price`, `Comfirmation_NO`, `Status`, `Delivery_Date`, `Receiver`, `Comments`, `References` From `jolab_ordering` where `jolab_ordering`.`ID`=117

***

I would appreciate if you could figure out what's wrong in my code. Thanks.

A
alang 10/10/2007

Bit hard to say - typically get this sort of error with any small mistake in the SQL - punctuation, spelling etc.

  • do you have spelling of all field names exactly right? (ie is it comfirmation or confirmation)
  • take the SQL and test it using a database tool like SQLyog
  • put echo statements in your code to check the parameters being passed ($strjolab_ordering and $strWhereClause)
  • failing that you may be able to post your project for the folks at xlinesoft.com to check for you

A
alang 10/10/2007

One other thing is that your SQL does not need to be so complicated. You are only looking for the date so another way would be:
$str = "SELECT Purchase_Date FROM ".$strjolab_ordering." WHERE ".$strWhereClause;

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs); // <<< note change here

if ($data[0]=="2007-01-01")

$smarty->assign("value_Purchase_Date",now());

L
laonian 10/11/2007

One other thing is that your SQL does not need to be so complicated. You are only looking for the date so another way would be:

$str = "SELECT Purchase_Date FROM ".$strjolab_ordering." WHERE ".$strWhereClause;

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs); // <<< note change here

if ($data[0]=="2007-01-01")

$smarty->assign("value_Purchase_Date",now());



Many thanks, AlanG. Tried your new code and ran into the same error.

Admin 10/11/2007

I suppose this line is wrong:

$str = "SELECT Purchase_Date FROM ".$strjolab_ordering." WHERE ".$strWhereClause

Should be

$str = "SELECT Purchase_Date FROM strjolab_ordering WHERE ".$strWhereClause

A
alang 10/11/2007

Good point Alexey - or most probably:
$str = "SELECT Purchase_Date FROM jolab_ordering WHERE ".$strWhereClause

L
laonian 10/11/2007

$str = "SELECT Purchase_Date FROM jolab_ordering WHERE ".$strWhereClause;



is the right one. Thank you guys for the great help.