Checking for empty field

I have a form and want to check if one of the fields (“namn”) are empty, so I use an IF statement:


$namn=$_POST['namn'];
if(empty($namn )) 
{
header('Location: no_article_submitted.php');	
exit();
}
else
{
$namn = mysqli_real_escape_string($link, $_POST['namn']);
$text = mysqli_real_escape_string($link, $_POST['text']);
$datum =date("Y-m-d,h:i:s");
$sql = 'INSERT INTO control SET
namn="' . '<h3>' . $namn . '</h3>' . '",
datum="' . $datum . '",
text="' . '<p>' . $text . '</p>' . '"';
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted article: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}

If the namn field is empty, I want to send the visitor to page no_article_submitted.php. If the namn field is not empty, I want to insert the new article in the database.

What happens? If I try to submit an empty form, the script sends me to the no_article_submitted.php. Thats good. But if I type something in the namn field and click submit, I will a) still be sent to no_article_submitted.php, and b) the database will be updated.

What am I doing wrong?

Thank you, BlaineSch!
The problem was this line:


header('Location: .');


how about


if(!$namn){

}

When sending forms I believe all fields are “set” but may not be set to anything. For instance if you turn it to “GET” even if you don’t fill out the field, the field would still display in the address bar.

Try using:

if(strlen($namn)==0)

$namn= (isset($_POST['namn']) && strlen($_POST['namn']) > 0) ? $_POST['namn'] : null;
if(is_null($namn )) {
header('Location: no_article_submitted.php');   
exit();
}
...rest of codes