...

View Full Version : What am I doing wrong?



djdubuque
01-08-2012, 10:45 AM
I would like to have the time and date added to my dB when my form is started. This should be UTC/GMT time.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
<title>Add entry</title>
</head>

<body>
<a href="add.php">Add entry</a><br>
<br>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 W4DJD Design
/// All rights reserved.

include("connect.php");

$query = "SELECT * FROM netform ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();

if ($num > 0 ) {
$i=0;
while ($i < $num) {
$date = time()+date("Z");
$callsign = mysql_result($result,$i,"callsign");
$name = mysql_result($result,$i,"name");
$location = mysql_result($result,$i,"location");
$msg = mysql_result($result,$i,"msg");
$id = mysql_result($result,$i,"id");


#echo "<b>date:</b> $date<br>";
echo gmdate("Y/m/d H:i:s",$date);
echo "<b>callsign:</b> $callsign<br>";
echo "<b>name:</b> $name<br>";
echo "<b>location:</b> $location<br>";
echo "<b>msg:</b> $msg<br>";
echo "<a href=\"update.php?id=$id\">Update</a> - <a href=\"delete.php?id=$id\">Delete</a>";
echo "<br><br>";

++$i; } } else { echo "The database is empty"; }?>
</body>
</html>

devinmaking
01-08-2012, 11:11 AM
I would like to have the time and date added to my dB when my form is started. This should be UTC/GMT time.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
<title>Add entry</title>
</head>

<body>
<a href="add.php">Add entry</a><br>
<br>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 W4DJD Design
/// All rights reserved.

include("connect.php");

$query = "SELECT * FROM netform ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();

if ($num > 0 ) {
$i=0;
while ($i < $num) {
$date = time()+date("Z");
$callsign = mysql_result($result,$i,"callsign");
$name = mysql_result($result,$i,"name");
$location = mysql_result($result,$i,"location");
$msg = mysql_result($result,$i,"msg");
$id = mysql_result($result,$i,"id");


#echo "<b>date:</b> $date<br>";
echo gmdate("Y/m/d H:i:s",$date);
echo "<b>callsign:</b> $callsign<br>";
echo "<b>name:</b> $name<br>";
echo "<b>location:</b> $location<br>";
echo "<b>msg:</b> $msg<br>";
echo "<a href=\"update.php?id=$id\">Update</a> - <a href=\"delete.php?id=$id\">Delete</a>";
echo "<br><br>";

++$i; } } else { echo "The database is empty"; }?>
</body>
</html>


On the date i would use this:

$date = gmdate("Y-m-d H:i:s");

Also do you mean
++$i should be $i++

djdubuque
01-08-2012, 11:35 AM
On the date i would use this:

$date = gmdate("Y-m-d H:i:s");Also do you mean
++$i should be $i++

You maybe right but this doesn't explain how the date will be added to the dB. Right now it only adds 00:00:00:00 to the dB not the current date and time the for was filled in.

devinmaking
01-08-2012, 11:47 AM
You maybe right but this doesn't explain how the date will be added to the dB. Right now it only adds 00:00:00:00 to the dB not the current date and time the for was filled in.

This would not insert into a database, this is just to retrieve the data...

To insert into a database, as you have used linkes you can do this.


if(isset($_REQUEST["mode"]) && $_REQUEST["mode"] == "add"){

//then enter the insert into database sql query.

}

Simply take all the variables and insert into database

Here is a link to show how to insert into query (http://www.w3schools.com/php/php_mysql_insert.asp)

Hope this helps

djdubuque
01-08-2012, 11:52 AM
I think I have that in my added.php file


<a href="index.php">Back to List</a>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 W4DJD Design
/// All rights reserved.

include("connect.php");
$date = trim($_POST['date']);
$callsign = trim($_POST['callsign']);
$name = trim($_POST['name']);
$location = trim($_POST['location']);
$msg = trim($_POST['msg']);

$query = "INSERT INTO netform (id, date, callsign, name, location, msg)
VALUES ('', '$date', '$callsign', '$name', '$location', '$msg')";
$results = mysql_query($query);

if ($results)
{
echo "Details added.";
}
mysql_close();
?>

Dan13071992
01-08-2012, 01:19 PM
can i just ask, what are you using the trim function here:



$date = trim($_POST['date']);
$callsign = trim($_POST['callsign']);
$name = trim($_POST['name']);
$location = trim($_POST['location']);
$msg = trim($_POST['msg']);


shouldnt it be:



$date = trim($_POST['date']);
$callsign = trim($_POST['callsign']);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$msg = strip_tags($_POST['msg']);


the reason im leaving the top two as trim is that unless you are calling the date from the form, there isnt really any need to have the $date stripped/trimmed.

just incase im wrong here, what is the trim function actually for?

tangoforce
01-08-2012, 01:59 PM
Can I just chime in here about storing the date/time in the database..

Whilst it without a doubt looks pretty to store a date and time in the database in a date and tiem format, it makes things difficult should you wish to sort the rows by the date and time.

Therefore I always recommend that you store the date and time as a timestamp directly from time() instead. When you pull out that record you can then format it any way you want using date().

There is an additional avantage to this, you can support users from multiple countries who may want to choose their own timezone in your program. Again using a timestamp gets around this whereas using a pre-formatted date screws that up too.

djdubuque
01-08-2012, 03:29 PM
can i just ask, what are you using the trim function here:



$date = trim($_POST['date']);
$callsign = trim($_POST['callsign']);
$name = trim($_POST['name']);
$location = trim($_POST['location']);
$msg = trim($_POST['msg']);
shouldnt it be:



$date = trim($_POST['date']);
$callsign = trim($_POST['callsign']);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$msg = strip_tags($_POST['msg']);
the reason im leaving the top two as trim is that unless you are calling the date from the form, there isnt really any need to have the $date stripped/trimmed.

just incase im wrong here, what is the trim function actually for?

Removes any excess whitespace on either side of my string. So it would remove any spaces before and after any actual data in the string.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum