Jump to content


Photo

when empty - prevent insert, but display a message


  • Please log in to reply
4 replies to this topic

#1 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 20 January 2013 - 10:20 AM

I've taken this as an extract of a much larger file.

If $score1 is empty the no score message appears, the exit() occurs. and the footer div will not appear.
If $score1 is filled, the success message and the footer div both appear fine.

If I remove the exit, then both the success and the no score message appears, and the footer div displays fine.

I would like the no score message to appear when $score1 is empty, aswell as the footer div to appear.

How should I restructure my code so that is so?

<input type="text" name="score"/>


<?php
if(isset($_POST['processForm']))
{
$score1 = $_POST['score'];
if (empty($score1)) {echo "You haven't entered a score for score"; exit();
}

#connection here
$sql = "INSERT STATEMENT HERE";
mysql_query($sql);

echo "success message";

};
?>
</form>
<div id="footer>...

Edited by wright67uk, 20 January 2013 - 10:24 AM.


#2 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 20 January 2013 - 10:51 AM

The method that we generally recommend is to use an array to hold your application error messages. You would add each error message as an element to the array.

You can then test at any point if the array is empty or not to decide if you want to do anything.

After you have performed all your validation tests, if the array is not empty, you would loop over the entries in it and output the error messages. if it is empty, you would form and run your query and if the query is successful and actually inserts a row, you would output your success message.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#3 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 20 January 2013 - 01:12 PM

Thankyou for the reply. And out of interest what would be the method if there were only 2 or even just 1 input?

#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,737 posts
  • LocationCanada

Posted 20 January 2013 - 01:43 PM

Thankyou for the reply. And out of interest what would be the method if there were only 2 or even just 1 input?


Create a standard process for doing your form validations and error handling - then just use it. You *could* modify the logic if you only have one or two fields, but why?
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 20 January 2013 - 02:07 PM

Well I just wasn't sure if you would make an array with just one value... Although I'm happy listening to the experts :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com