View Full Version : Date/Time not displaying in Edit mode

06-28-2010, 10:58 PM
I have a page where you can post and edit a listing. However when you go into edit mode, all the fields appear filled in with the previous data except for the date and time. I know this is probably something to do with the format of each of these but I can't work out how these should be labelled in the code.
I've tried different ways except for the correct one it would seem.
Can anyone out there help please?


else: // Edit listings
$id = $_GET['id'];
$listings = @mysql_query (
"SELECT id, day, date_format(date,'%Y/%m/%d') as d, channel, programmetitle, time_format

(starttime, '%h:%i%p') as t, duration, notes FROM listings WHERE id='$id'");
if (!$listings) {
exit ('<p>Error fetching listings: ' .
mysql_error() . '</p>');

$listings = mysql_fetch_array($listings);

function safe($value){
return mysql_real_escape_string($value);


$id = $listings['id'];
$day = $listings['day'];
$date = $listings['date'];
$channel = $listings['channel'];
$programmetitle = $listings['programmetitle'];
$starttime = $listings['starttime'];
$duration = $listings['duration'];
$notes = $listings['notes'];

//Convert special characters for safe use
$id = htmlspecialchars($id);
$day = htmlspecialchars($day);
$date = htmlspecialchars($date);
$channel = htmlspecialchars($channel);
$programmetitle = htmlspecialchars($programmetitle);
$starttime = htmlspecialchars($starttime);
$duration = htmlspecialchars($duration);
$notes = htmlspecialchars($notes);

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<legend>Enter listings</legend>
<div><label for="day">Day:</label> <br /><input id="day" type="text" name="day" value="<?php

echo $day; ?>"></div>
<div><label for="date">Date:</label><br /><input id="date" type="text" name="date" value="<?php

echo $date; ?>" /></div>
<div><label for="channel">Channel:</label> <br /><input id="channel" type="text" name="channel"

value="<?php echo $channel; ?>" /></div>
<div><label for="prog_title">Prog Title:</label><br /> <textarea id="prog_title"

name="programmetitle" rows="1" cols="70"><?php echo $programmetitle; ?></textarea></div>
<div><label for="starttime">Start time:</label><br /> <input id="starttime" type="text"

name="starttime" value="<?php echo $starttime; ?>" ></div>
<div><label for="duration">Duration:</label> <br /><input id="duration" type="text"

name="duration" value="<?php echo $duration; ?>" ></div>
<div><label for="notes">Notes:</label><br /> <textarea id="notes" name="notes" rows="5"

cols="70"><?php echo $notes; ?></textarea></div>
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<div><input type="submit" value="SUBMIT"></div>

Thank you

06-28-2010, 11:15 PM
You're using the wrong names in your array index:

//date_format(date,'%Y/%m/%d') as d
$date = $listings['date'];

//time_format(starttime, '%h:%i%p') as t
$starttime = $listings['starttime'];

When you SELECT `something` AS `something_else`, you're returning the column name as "something_else", not the original "something". Thus, you need to be using "d" and "t" as your array keys in those two lines, I think.

EDIT: By the way, "date" is a reserved word in MySQL (and probably other systems). If you must use reserved words as fields names, wrap them in backticks/graves (that's the "`" character to the left of the number 1 key on a standard US keyboard) when using those names in queries.

06-28-2010, 11:30 PM
That works perfectly. Thank you.
And it makes sense now I've stepped back from it and er... had it pointed out to me.

Many many thanks