Jump to content

[SOLVED] Date Giving problem in PHP


gc40

Recommended Posts

Well, your gonna have to look at the date / time functions for PHP and decide on the best method

 

http://us3.php.net/datetime

 

I personally don't use this kind of format as it get cumbersome trying to keep the damn thing in the proper formatting. I like to use the unix timestamp. one string of numbers gives me hours, seconds, minutes, day, month and year. I can then control how it is displayed by using the date() function after I pull it from the database. It is easy to create new string dates with mktime(). Once you get past the learning curve of em, they are the best way (in my opinion) to deal with any kind of date / time issue.

Link to comment
Share on other sites

Chron, my add nav script allows me to add the date succesffuly without any of those.

It works great and I can add new entries with successful display of the Dates. Would you like to see it? I just need to figure out why it works for the adding script and not for the updating script!

Link to comment
Share on other sites

ADD SCRIPT

<?php
// stop errors on multiple session_start()
include ("../class/config.php");
$today = date("Y-m-d");

/* If form hasn’t been submitted */
if(!isset($_POST['submit']))
{ 
$nav="";
$netasset="";
list($year, $month, $day) = explode ("-", date("Y-m-d"));
PrintNAV($nav,$netassets,$id,$year,$month,$day, "ADD NAV");
?>


<?php

}
// If form has been submitted
else
{
$netassets=$_POST['netassets'];
$nav=$_POST['nav'];
$year=$_POST['year'];
$month=$_POST['month'];
$day=$_POST['day'];

$error = mysql_query($sql, $db);
if (trim($error)=="")
{
	// Prepare variables
	$nav =  QuoteSmart($nav);
	$netassets =  QuoteSmart($netassets);
	$postdate = $year."-".$month."-".$day;
	$query = "insert into navs (Date, NetAssets, Nav) values ('$postdate', '$netassets', '$nav')";

	if (mysql_query($query) ) {
		echo "<p class=\"success\">Nav item - ".stripslashes($nav)." (".DisplayDate(stripslashes($postdate)).") - added.</p>\n";
	} 
	else echo "<p class=\"error\">Error adding news item:<br />" .mysql_error(). "</p><p>".$nav."\n";
}
else echo "<p class=\"error\">$error</p>";
}
?>
<?php 
function PrintNAV($nav,$netassets,$id,$year,$month,$day,$submit_value) {
?>
<form action= "<?php $_SERVER['PHP_SELF'];?>" method="POST">
<input type='hidden' name='action' value='addnav'>
<table>
    <tr><td>Day:</td><td>
    <select name="day" id="day">
    <?php
    for ($i = 1; $i <= 31; $i++)
    {
        if ($i == $day) echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
        else echo "<option value=\"$i\">$i</option>\n";
    }
    ?>
    </select> 
    </td>
    </tr>
    <tr>
        <td>Month:</td><td> 
        <select name="month" id="month">
        <?php
            $monthList=array(
            1 => "January",
            2 => "February",
            3 => "March",
            4 => "April",
            5 => "May",
            6 => "June",
            7 => "July",
            8 => "August",
            9 => "September",
            10 => "October",
            11 => "November",
            12 => "December");
        
        foreach($monthList as $code => $monthname)
        {
            if ($code == $month) echo "<option value=\"$code\" selected=\"selected\">$monthname</option>\n";
            else echo "<option value=\"$code\">$monthname</option>\n";
        
        }
        ?>
        </select> 
        </td></tr>
        <tr>
        <td>Year:</td><td>
        <select name="year" id="year">
        <?php
        $this_year = date("Y");
        for ($i = $this_year; $i <= $this_year+10; $i++)
        {
            if ($i == $year) echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
            else echo "<option value=\"$i\">$i</option>\n";
        }
        ?>
        </select></td>
        </tr>
<tr><td>Net Assets</td><td><input type="text" name="netassets"></td></tr>
<tr><td>Nav </td><td><input type="text" name="nav"></td></tr>
<tr><td><input type="submit" value="Add" name="submit" class="submit button"></td><td><input type="submit" value="Cancel" name="submit" class="submit button"></td></tr>
</table>


</form>
<?php
}
?>

 

EDIT/VIEW/DELETE SCRIPT (date update to database not working)

<?php
//error_reporting(E_ALL);
include ("../class/config.php");
$today = date("Y-m-d");

$submit=$_REQUEST['submit'];
$id=$_REQUEST['id'];
$del=$_REQUEST['del'];
if(isset($submit))
{
$fulldate = $year . "-" . $month . "-" . $date;

  if($submit == 'Delete')
{
	$sql = "delete from navs where id = '$id'";

	$result = mysql_query($sql, $db);

	if($result)
	{
		echo("The NAV was deleted successfully.");
		echo("<form action='nav_index.php?action=editnav' method='POST'><p>");
		echo("<input type='submit' name='submit' value='OK' class=\"submit button\"></form)");
	}

}

else if($submit == 'Update')
{
	$netassets=$_POST['netassets'];
	$nav=$_POST['nav'];

$sql = "update navs set NetAssets='$netassets',Nav='$nav', Date='$postdate' where id = '$id'";

echo $sql;

	$result = mysql_query($sql, $db);
	if($result)
	{
		echo $sql;
		echo("The NAV was updated successfully.");
		echo("<form action='nav_index.php?action=editnav' method='POST' onSubmit='fnsubmit()'>");
		echo("<input type='submit' name='submit' value='OK' class=\"submit button\"></form)");
	}

}



else if (($submit == 'Cancel') || ($submit == 'OK'))
{ 	//header("Location:http://www.bviddm.com/control/nav_index.php?action=editstory"); 
	?>
	<script language="Javascript" type="text/javascript"> 
	document.location.href='nav_index.php?action=editnav'
	</script>
   <?
}
}


else
{

if(isset($del))
{
	$sql = "select * from navs where id = '$id'";
	$navset = mysql_query($sql, $db);
	$onenav = mysql_fetch_object($navset);
	echo ($sql);
	echo("Are you sure you want to delete the NAV ");
	echo($onenav->id);
	echo("?");
	echo("<form action='nav_index.php?action=editnav' method='POST' onSubmit='fnsubmit()'>");
	echo("<input type='hidden' name='id' value=" . $onenav->id . ">");
	echo("<table><tr><td><input type='submit' name='submit' value='Delete' class=\"submit button\"></td><td><input type='submit' name='submit' value='Cancel' class=\"submit button\"></td></tr></table></form)");
}
else
{
	if(isset($id))
	{
$sql = "select * from navs where id = '$id'";
$postdate = $year."-".$month."-".$day;
$navset = mysql_query($sql, $db);
$onenav = mysql_fetch_object($navset);
list($year, $month, $day) = explode ("-", date("Y-m-d"));
		?>
<?php echo($sql); ?>			
<form action= "nav_index.php?action=editnav" method="post">
    <input type='hidden' name='id' value=<?php echo($id); ?>>
    <table>
    <tr><td>Day:</td><td>
    <select name="day" id="day" value=<?php echo($day); ?>>
    <?php
    for ($i = 1; $i <= 31; $i++)
    {
        if ($i == $day) echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
        else echo "<option value=\"$i\">$i</option>\n";
    }
    ?>
    </select> 
    </td>
    </tr>
    <tr>
        <td>Month:</td><td> 
        <select name="month" id="month" value=<?php echo($month); ?>>
        <?php
            $monthList=array(
            1 => "January",
            2 => "February",
            3 => "March",
            4 => "April",
            5 => "May",
            6 => "June",
            7 => "July",
            8 => "August",
            9 => "September",
            10 => "October",
            11 => "November",
            12 => "December");
        
        foreach($monthList as $code => $monthname)
        {
            if ($code == $month) echo "<option value=\"$code\" selected=\"selected\">$monthname</option>\n";
            else echo "<option value=\"$code\">$monthname</option>\n";
        
        }
        ?>
        </select> 
        </td></tr>
        <tr>
        <td>Year:</td><td>
        <select name="year" id="year" value=<?php echo($year); ?>>
        <?php
        $this_year = date("Y");
        for ($i = $this_year; $i <= $this_year+10; $i++)
        {
            if ($i == $year) echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
            else echo "<option value=\"$i\">$i</option>\n";
        }
        ?>
        </select></td>
        </tr>    <tr><td>Net Assets</td><td><input type="text" name="netassets" value=<?php echo($onenav->NetAssets);?>></td></tr>
    <tr><td>Nav </td><td><input type="text" name="nav" value=<?php echo($onenav->Nav);?>></td></tr>
    <tr><td colspan="2"><input type="submit" value="Update" name="submit" class="submit button"></td></tr>
    </table>
</form>
<?php
	}

	else 
	{
?>

<table width="100%">
<tr><th class='right_title'>Date</th><th class='right_title'>Net Assets</th><th class='right_title'>Nav</th>
<th colspan=2><center>Action</center></th></tr>

<?php

$navquery = "select * from navs";

$navresult = mysql_query($navquery);
while ($row = mysql_fetch_object($navresult))
{
echo ("<tr>"."<td>".$row->Date."</td>"."<td>".$row->NetAssets."</td>"."<td>".$row->Nav."</td>");

echo("<td>"."<p align='center'>"."<a href='nav_index.php?action=editnav&id=$row->id'>"."<img src='..\images\button_edit.png' border='0' alt="."Edit".">"."</td>".
"<td><p align='center'>"."<a href='nav_index.php?action=editnav&id=$row->id&del=$row->id'>"."<img src='..\images\button_drop.png' border='0' alt="."Delete"."></td></tr>");
}

	}
?>


</table>
<?php

}} ?>

Link to comment
Share on other sites

Can you suggest a solution, I am completely lost. I have tried everything I KNOW. I don't know what else to do to get postdate to output the date.

 

p.s. It is in the right format, just not outputting a value when updating, only a NULL value (00-00-0000)

 

No it's NOT in the right format.  Your query showed up as this:

 

update navs set NetAssets='4',Nav='4', Date='' where id = '40'update navs set NetAssets='4',Nav='4', Date='' where id = '40'

 

This means that your $postdate variable is empty.  Empty is not the correct format.  You need to determine why it is empty.

 

It looks like you use day, month and year as your form variables.  That means you need to take the variables $_REQUEST['day'], $_REQUEST['month'] and $_REQUEST['year'] and construct your date from them.

Link to comment
Share on other sites

You need this line

 

$postdate = $year."-".$month."-".$day;

 

That line is in your add script, but missing from your update script.  It must come after you set $year, $month and $day, and before you use $postdate in your $sql.

Link to comment
Share on other sites

When problems arise, get into the habit of a systematic troubleshooting approach. If data is not getting to the database like you expect, then do the following.

 

echo out the queries that your using to "talk" to the database. Generally this one step will show you what the problem is. Once you have that done and you can identify where the query is not correct, then you need to go backwards to that variable and see why it is not populating correctly.

 

A lot of times a simple echo 'Part xxxx worked' is a good start. Especially when dealing with if/else statements. When these don't work, then you need to start inside the statement and echo things so you can see if the statement is running like you expect.

 

Lots of problems can be solved by simply using echo. Make sure vars are set correctly, make sure queries are correct, and make sure your logic is working like you expect.

 

Glad you got it figured out.

 

Nate

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.