Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Daniphord Mwajah
Daniphord Mwajah
186 Points

!empty() function lies

I have a problem post my data to database, every time I try to submit data to my data base the !empty function lies, it says that all the fields need to be field, while in fact they are! I don't know what is wrong with my code I even tried to echo the values from the form and they all print fine, but still my code tells me that All fields need to be filled. Below is the code:

<?php
$date = @$_POST['date'];
$month = @$_POST['month'];
$year = @$_POST['year'];
$group = @$_POST['group'];
$name = @$_POST['teachername'];
$boys = @$_POST['boysnum'];
$girls = @$_POST['girlsnum'];                             
$total = @$_POST['total'];
$sponsored = @$_POST['sponsored'];
$lunchtakers = @$_POST['lunchtakers'];
$comment = @$_POST['comment'];
$recordby = @$_SESSION['userName'];

echo $date."<br>";
echo $month."<br>";
echo $year."<br>";
echo $group."<br>";
echo $name."<br>";
echo $boys."<br>";
echo $girls."<br>";
echo $sponsored."<br>";
echo $lunchtakers."<br>";
echo $comment."<br>";
echo $recordby."<br>";

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

if(!empty($date) && !empty($month)  && !empty($year) && !empty($group) && !empty($name) && !empty($boysnum) && !empty($girlsnum) && !empty($total) && !empty($sponsored) && !empty($lunchtakers) && !empty($comment) && !empty($recordby)) {

$insert = "INSERT INTO `khmattendance`( `Date`, `Month`, `Year`, `Groupname`, `Teachersfullname`, `Numberofboys`, 
`Numberofgirls`, `Totalnumber`, `Sponsoredchildren`, `Lunchtakers`, `Teacherscomment`, `Addedby`) VALUES (

        '".mysqli_real_escape_string($connect, $date)."',
        '".mysqli_real_escape_string($connect, $month)."',
        '".mysqli_real_escape_string($connect, $year)."',
        '".mysqli_real_escape_string($connect, $group)."',
        '".mysqli_real_escape_string($connect, $name)."',
        '".mysqli_real_escape_string($connect, $boys)."',
        '".mysqli_real_escape_string($connect, $girls)."',
        '".mysqli_real_escape_string($connect, $total)."', 
        '".mysqli_real_escape_string($connect, $sponsored)."',
        '".mysqli_real_escape_string($connect, $lunchtakers)."', 
        '".mysqli_real_escape_string($connect, $comment)."',
        '".mysqli_real_escape_string($connect, $recordby)."')";


$result = mysqli_query($connect, $insert);

echo "<br><br>";
if($result){
    echo '<div class="div1">';
    echo '<br>';

    echo '<h1><strong>Data successfully inserted!</strong></h1><br><br>';

    echo '<a href="sponsorregister.php" id="backlink">Back to previous Page</a><br>'; 
echo"<br>"; echo "OR<br>";
echo '<a href="khmcorner.php" >Back to controllpanel</a><br>'; 

    echo '</div>'; 

  }else{
    echo "<br><br>";
    echo '<div id="div1">';
    echo '<br><br>';

    echo '<h1><strong>Data insertion was unsuccessful!<strong></h1></div><br><br><br>';

    echo '<a href="sponsorshipaccountsfeeder.php" id="backlink">Back to previous Page</a><br>'; 
    }

 }else{
    echo "<br><br>";
    echo '<div id="div1">';
    echo '<br><br>';
    echo '<h1><strong>All fields need to be filled!<strong></h1></div><br><br><br>';
    echo '<a href="sponsorshipaccountsfeeder.php" id="backlink">Back to previous Page</a><br>'; 
}

}
Sean T. Unwin
Sean T. Unwin
28,690 Points

I edited your post slightly to make it a bit more readable.

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

The variables $boysnum and $girlsnum do not exist. They are declared as $boys and $girls. This means that in the if statement with all the ! empty()'s is failing.

/*...*/ !empty($boysnum) && !empty($girlsnum) /*...*/
//should be:
/*...*/!empty($boys) && !empty($girls) /*...*/

To be clear, ! empty() is not lying to you. :wink:

Daniphord Mwajah
Daniphord Mwajah
186 Points

Thank you very much Sean, I now understand why! Your answers were very helpful.