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

Edited by wright67uk
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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