Question : If I don't fill in all text fields, then the ones I left blank insert as empty records

I have 5 textfields. If I fill in all 5 it inserts 5 new rows into the table which is great. If i only fill in 3 text fields it still does 5 rows, but 2 of them are blank. How can I edit this code so that it won't insert blank text fields?

$dbc = mysql_connect('localhost', 'root', 'root')
or die ('Cannot connect');
mysql_select_db("video", $dbc);

if (isset($_POST['Submit'])) {
foreach ($_POST['genre'] as $genre_insert) {
$query = "INSERT INTO genre(Genre) VALUES('$genre_insert')";
$result = mysql_query($query, $dbc);
}
 }
 mysql_close($dbc);

Answer : If I don't fill in all text fields, then the ones I left blank insert as empty records

Sorry, somehow managed to screwup the above post. Meant to write:

This should do the job:

$dbc = mysql_connect('localhost', 'root', 'root')
or die ('Cannot connect');
mysql_select_db("video", $dbc);

if (isset($_POST['Submit'])) {
foreach ($_POST['genre'] as $genre_insert) {
if (strlen($genre_insert)!=0){
$query = "INSERT INTO genre(Genre) VALUES('$genre_insert')";
$result = mysql_query($query, $dbc);
}
}
 }
 mysql_close($dbc);
Random Solutions  
 
programming4us programming4us