Results 1 to 2 of 2

Thread: [RESOLVED] Date and Order

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] Date and Order

    How do i put the current date into a field in mysql? and how do i show them from most current post at top and oldest at the bottom.

    it displays the Date as: "0000-00-00"

    Here is the code i am using for POSTING:
    PHP Code:
    <?PHP
    if(isset($_POST['subject'], $_POST['content'], $_POST['poster'])) { 
                        
    //form was submitted, do some error checking on the variables submitted and add in your query here 
                        //for example: 
                        
    $db mysql_connect('localhost''homtek_dclamp''moiola') or die(mysql_error()); 
                        
    mysql_select_db('homtek_homtek'$db) or die(mysql_error()); 
                        
                        
    $sContent mysql_real_escape_string($_POST['content']); 
                        
    $sPoster mysql_real_escape_string($_POST['poster']); 
                        
    $sSubject mysql_real_escape_string($_POST['subject']); 
                        
                        
    $query "INSERT INTO news VALUES ('', '$sContent', '$sPoster', '$sSubject', 'NOW(YYYY-MM-DD)')"
                
                        
    mysql_query($query$db) or die(mysql_error()); 
                        echo 
    "News Post has been added to database.<br><br>"
                    echo 
    "Subject: "$_POST['subject'] ,"<br>";
                    echo 
    "Content: "$_POST['content'] ,"<br><br>";
                        echo 
    "[<a href='main.php'>Main Page</a>]"
                } else { 
                
    ?> 
                     <form action="add_news.php" method="POST">
                    Subject:<br>
                    <input type=text maxlength="25" name="subject"><br><br>
                    News Content:<br>
                    <textarea wrap=hard name="content" columns="5" rows="10"></textarea><br>
                    <input type="hidden" name="poster" value="<?PHP echo $logged_in_admin?>">
                    <input type="hidden" name="date" value="DATE_HERE">
                    <input type="submit" value="Submit"> <input type="reset" value="Reset Form">
                    </form> 
                    <br>[<a href="main.php">Main Page</a>]
                <? } ?>
    here is the code i am using to DISPLAY:
    PHP Code:
    <?php 
                    
    echo '<table width="620" border="1" bordercolor="white">';
                
    //build our query 
                
    $db mysql_connect('localhost''homtek_dclamp''moiola') or die(mysql_error()); 
                
    mysql_select_db('homtek_homtek'$db) or die(mysql_error()); 
                
    $query "SELECT * FROM `news` LIMIT 5 ORDER BY `news_id`"
                
    $query mysql_query($query$db) or die(mysql_error()); 
                
    //loop through each record 
                
    while($array mysql_fetch_array($query)) { 
                        
    //print out the field values and values using print_r 
                    
    echo '<tr>';
                    echo 
    '<td width="100" border="1" bordercolor="black">';
                        
    print_r($array['date']."<br>"); 
                        
    print_r($array['poster']."<br>"); 
                        echo 
    '</td>';
                        echo 
    '<td width="500" border="1" bordercolor="black">';
                        
    print_r("<b>".$array['subject']."</b><br><br>"); 
                        
    print_r($array['news']."<br>"); 
                        echo 
    '</td>'
                               echo 
    '</tr>';
                } 
                echo 
    '</table>';
                echo 
    '<br>[<a href="news.php">View All News</a>]';
                
    ?>
    My usual boring signature: Something

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date and Order

    in your display code, change your query to:
    PHP Code:
    $query "SELECT * FROM `news` ORDER BY `news_id` DESC LIMIT 5"
    I don't know the syntax for the NOW function in MySQL and don't feel like looking it up right now either, so you could always just insert the date using PHP.. here's an example for when you're inserting your data:
    PHP Code:
    $query "INSERT INTO news VALUES ('', '$sContent', '$sPoster', '$sSubject', '" .  date("Y-m-d") . "')"
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width