473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

if statement, message not being output

23
Hi
I have a script that checks whether a user is logged in or not.

If the user are not logged in they are directed to the login page.

else the user is logged in.

If the user is logged in they can adda story,

I have a validation check on the title and main text fields, if the title is not set, then output an error message eg
Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['title'])) {
  2. // Do this
  3. $t = escape_data($_POST['title']);
  4. } else { 
  5. // Output error message
  6. $t = FALSE;
  7. echo '<p>You forgot to enter a title</p>';
  8. }
  9.  
however this message is not being ouput and I can't figure out why.

Any help would be appreciated

Here is the full code:
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. # Filename - add_story.php
  3. # Date - 9th August 2007
  4. # Author - Stephen Hoult
  5. # Author Email - stephen@hoult.org.uk
  6.  
  7. // This file allows logged in users to add a story.
  8.  
  9. // Include config file for error management and such
  10. include('./includes/config.inc.php');
  11.  
  12. // Set page title and include HTML header
  13. $page_title = 'Add a Story';
  14. include('./includes/header.html');
  15.  
  16.  
  17. // If no first_name variable exists, redirect the user.
  18. if (!isset($_SESSION['first_name'])) {
  19.  
  20.     // Start defining the URL.
  21.     $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
  22.     // Check for a trailing slash.
  23.     if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
  24.         $url = substr ($url, 0, -1); // Chop off the slash.
  25.     }
  26.     // Add the page.
  27.     $url .= '/login.php';
  28.  
  29.     ob_end_clean(); // Delete the buffer.
  30.     header("Location: $url");
  31.     exit(); // Quit the script.
  32.  
  33. } else { // First name variable exists - user is logged in
  34.  
  35.                     //If the form has been submitted
  36.                     if (isset($_POST['submitted'])) {
  37.  
  38.  
  39.  
  40.                         // Connect to the database
  41.                         require_once('../mysql_connect_ur.php');
  42.  
  43.  
  44.                             // Validate title
  45.                             if (isset($_POST['title'])) {
  46.                                 $t = escape_data($_POST['title']);
  47.                             } else {
  48.                                 $t = FALSE;
  49.                                 echo '<p><font color="red" size="+1">Please enter a title.</font></p>';
  50.                             } // End of validate Title
  51.  
  52.                             // Validate Main text
  53.                             if (isset($_POST['title'])) {
  54.                                 $mt = escape_data($_POST['main_text']);
  55.                             } else {
  56.                                 $mt = FALSE;
  57.                                 echo '<p>You forgot to enter the main text.</p>';            
  58.                             } // End of validate Main text
  59.  
  60.  
  61.                                     if  ($t && $mt) { // If validation checks pass
  62.  
  63.                                         // Add the story.
  64.                                         $query = "INSERT INTO content (title, main_text, date_submitted) 
  65.                                                   VALUES ('$t', '$mt', NOW())";
  66.                                         $result = mysql_query($query);
  67.                                         echo '<p>Your story has been added to the database</p>';
  68.                                         mysql_close(); // Close database connection
  69.  
  70.                                     } else { // Validation check failed
  71.                                         echo '<p>A system error has occurred. Your story has not been added.</p>';
  72.  
  73.                                     } // End of if all validation checks pass
  74.  
  75.  
  76.                     } // End of if Submitted    
  77. } // End of is logged in
  78. ?>
  79.  
  80.  
  81. <fieldset><legend>Add a story</legend>
  82.  
  83. <form action="add_story.php" method="post" >
  84.  
  85.     <p>Title: <input type="text" name="title" size="30" maxlength="50" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" /></p>
  86.  
  87.     <p>Main Text<textarea name="main_text" cols="40" rows="5"/><?php if (isset($_POST['main_text'])) echo $_POST['main_text']; ?></textarea></p>
  88.  
  89.     <p><input type="submit" name="submit" value="Submit" /></p>
  90.     <input type="hidden" name="submitted" value="TRUE" />    
  91.  
  92. </form>
  93.  
  94. </fieldset>
  95.  
  96. <?php
  97. include('./includes/footer.html'); // Include HTML footer
  98. ?>
  99.  
Aug 9 '07 #1
2 1926
keeps21
23
SOLVED

I should have been using:

Expand|Select|Wrap|Line Numbers
  1. if (!empty($_POST['title']) { 
  2.      // Do this
  3. } else {
  4.      // Do this
  5. }
  6.  
rather than

Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['title']) { 
  2.      // Do this
  3. } else {
  4.      // Do this
  5. }
  6.  
Aug 9 '07 #2
Atli
5,058 Expert 4TB
Glad you solved your problem!
And thanks for sharing the solution.
Aug 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Toby Donaldson | last post by:
Hi all, I'm designing an educational application that will run Python code and check the output against a pre-define answer. I want to use the "exec" statement to run the code, but I don't know...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
5
by: John Morgan | last post by:
In a 'Top n' type statement I wish to be able to insert the n value from a parameter, within a stored precedure eg Having declared @pageSize as a parameter I want to run the following type of...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
3
by: Eric Lilja | last post by:
Hi, I need a function that expects a const std::string& containing the visual representation of a number in binary format. It should then output this number in groups of four bits separated by a...
3
by: Gordon Moore | last post by:
Hi, I'm new to using xml/xslt and although I can create an xml document using the dataset.WriteXml statement, and I have created an xslt to transform the xml into the output I want, I have to...
4
by: Polly | last post by:
I had a macro that ran a parameter query and created and opened an Excel file with the system date as part of the file name, but I had to change the file name by hand. So I converted the macro to...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
13
by: Jim | last post by:
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this...
2
by: JRough | last post by:
I have this code that switches templates depending on if the user fills in a form with a request. The request asks for the $mark & $number. If that request gets input then it displays a list...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.