Jump to content

when empty - prevent insert, but display a message


wright67uk

Recommended Posts

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>...

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.