SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Custom news admin script isn't working as expexcted :(

    Hello,

    I have a news admin page (news_admin.php) containing this code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>News Admin</title>
    </head>
    
    <body>
    
    <?php
    	//Database connection and selection
    	include '../inc/db.inc.php';
    	
    	if (isset($_GET['addnews'])):
    	//If the user wants to add a news
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Enter the headline of the news:<br />
    	<input name="newsheadline" type="text" size="64" />
    </label><br />
    <label>Date:<br />
    	<input name="newsdate" type="text" size="20" />
    </label><br />
    <label>News category ?<br />
    	<input name="newscategory" type="text" size="64" />
    </label><br />
    <label>Enter news text :<br />
    	<textarea name="newstext" rows="5" cols="80"></textarea>
    </label><br />
    <label><br />
    </label>
    	<input type="submit" value="save" />
    </form>
    
    
    <?php else: //Show all news already entered in database
    	
    	//If a news has been added to the form
    	//then it will be saved to the database
    		if (isset($_POST['newsheadline'], $_POST['newsdate'], $_POST['newscategory'], $_POST['newstext'])) {
    		$newsheadline = $_POST['newsheadline'];
    		$newsdate = $_POST['newsdate'];
    		$newscategory = $_POST['newscategory'];
    		$newstext = $_POST['newstext'];
    		
    		//If the date form field has been left empty, the current date should be input by default
    		if (empty($newsdate)) {
    			$sql = "INSERT INTO news SET
    					newsheadline='$newsheadline',
    					newsdate=CURDATE(),
    					newscategory='$newscategory',
    					newstext='$newstext'";
    			
    			} else {
    			$sql = "INSERT INTO news SET
    					newsheadline='$newsheadline',
    					newsdate=newsdate(),
    					newscategory='$newscategory',
    					newstext='$newstext'";
    			}
    		}
    		
    		if (@mysql_query($sql)) {
    			echo 'News has been successfully added!';
    		} else {
    			echo 'Adding news failed: ' . mysql_error() . '';
    		}
    echo '<p>Current news listing:</p>';
    
    //Display of all news in the database
    $newslist = @mysql_query('SELECT ID, newsheadline, newsdate, newscategory, newstext FROM news ORDER BY newsdate DESC LIMIT 3');
    if (!$newslist) {
    	exit('Display of news has failed: ' . mysql_error() . '');
    }
    
    //Formating news listing display:
    while ($row = mysql_fetch_array($newslist)) {
    	$newsheadline = $row['newsheadline'];
    	$newsdate = $row['newsdate'];
    	$newscategory = $row['newscategory'];
    	$newstext = $row['newstext'];
    	
    	echo( "<p>$newsheadline <br />" );
    	echo( "$newsdate $newscategory <br />" );
    	echo( "$newstext </p>" );
    }
    
    //If the user wants to add another news, the form will be displayed
    echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add news...</a></p>';
    
    endif;
    ?>
    
    </body>
    </html>
    The include contains:
    Code:
    <?php
    	 //Connection database
         $dbcnx = @mysql_connect('host', 'user', 'password');
         if (!$dbcnx) {
           echo 'Connecting to the database server has failed.';
           exit();
         }
    	 
    	 //Selection of the right database
    	 mysql_select_db('databasename',$dbcnx);
         if(!@mysql_select_db('databasename')) {
           exit('Selecting the database has failed.');
         }
    ?>
    Now if I load that script it displays:
    first line:
    Adding news failed: Query was empty

    Then it lists the already inserted news and at the bottom of that, it shows the "Add news..." link. Now when I click on that link in order to add a news to my database, the form won't be displayed at all except it seems like it just reloads the page without any change to the originally loaded page news_admin.php.

    I have no idea what's the problem, any suggestions?

    kind regards
    Marcin

    P.S.: I'm learning this from Kevin Yank's book "Build your own Database Driven Website using PHP & MySQL"

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    at first glance:
    Code:
    <p><a href="' . $_SERVER['PHP_SELF'] . '?addjoke=1">
    should be
    Code:
    <p><a href="' . $_SERVER['PHP_SELF'] . '?addnews=1">

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrgghhh!

    Silly me

    This typo did it for me.

    Thanks!


Bookmarks

Posting Permissions

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