Jump to content

check for blank fields


flemingmike

Recommended Posts

hi, is there a way to do a check for blank fields before posting to database?  dumb users keep adding blank fields.

 

i need to check fields $_POST[company], $_POST[jobNO] and $_POST[staff].

 

 

if(isset($_POST['add']))
{


$cname=$_POST[company];

$compid = mysql_query("SELECT * FROM company WHERE company = '$cname'");
while($row4 = mysql_fetch_array($compid))
{


$cid=$row4["ID"];

}




$query = "INSERT INTO jobno VALUES (
NULL, 
'$_POST[jobNO]', 
'$cid'
)";

mysql_query($query) or die('Error, insert failed');


$query1 = "INSERT INTO staff VALUES (
NULL, 
'$_POST[staff]', 
'$_POST[Y]-$_POST[M]-$_POST[D]', 
'$_POST[jobNO]'
)";

mysql_query($query1) or die('Error, insert failed1');


echo "1 record added";

Link to comment
Share on other sites

if((empty($_POST['company'])) || (empty($_POST['jobNO]')) || (empty($_POST[staff]))){
echo "Please fill in the fields, all are required";
exit;
}

 

That's the most obvious thing that comes to mind...

 

And pop error_reporting(E_ALL); on, as the script as it stands should have thrown an error, as you aren't quoting the array index's..

 

@mraza; with all due respect, that's not very friendly to the user, what if they only need to type in a two/three letter value, just thought I would point that out...

 

Rw

Link to comment
Share on other sites

Here ya go. Just repeat the if conditional for each field. Lemme know if you need help.

 

<?php
// After validating that form has been submitted . . .
array_map('trim', $_POST); // trim leading and trailing whitespace from all elements of $_POST array.

if( empty($_POST['field']) ) {
     // validation fails, do something evil, dastardly, nefarious, or just outright mean
} else {
     // validation passes, so do something completely different, or possibly nothing at all, depending on your mood.
}
?>

Link to comment
Share on other sites

@rwwd

 

well if i am not wrong in empty if a user give a space that would be counted as well so user can skip that checking and perform action so in my opinion is good to count there words length and to work with your code properly need to add trim check before you perform this code like

 

trim($_POST['company'])

 

then your code of checking ;)

Link to comment
Share on other sites

dumb users keep adding blank fields

 

I just gave what was asked, but yes you are right, but spaces have their ascii values too, so strlen() would have the same effect, realistically, preg_match would *technically* be the best course of action in this instance but I can't be bothered to work it out..

 

And as correctly pointed out array_maps callback feature is really good in this scenario, but it needs to be assigned to a var, as it returns an array - hence why I overwrite the $_POST in other threads, works perfectly well :)

 

Rw

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.