Jump to content

[SOLVED] Inserting empty data into mysql when not supposed to


chronister

Recommended Posts

Hi all,

 

I am creating an interface for my wife to keep track of what cross stitch threads she has. I have created a text area so she can enter a bunch at a time. I am trying to get the script to skip over blank lines. The code  for this piece is here:

 

<?php

if($_POST['add_floss']){ // submit button has been pressed

$num=nl2br($_POST['floss_num']); // grab the textarea data, run through new line 2 break
$name=nl2br($_POST['floss_name']);// grab the textarea data, run through new line 2 break

$floss_num=explode('<br />',$num); // explode on the <br>, create array
$floss_name=explode('<br />',$name); // explode on the <br>, create array

connectdb(); // call to connectdb function

foreach($floss_num as $v){ 

  if(isset($v)){ // if $v is not empty run query and insert

       $query="INSERT INTO floss (floss_num , floss_qty , floss_name ) VALUES ('$v', '1', NULL)";
       $result=mysql_query($query);

   }//end if $v
}//end foreach

foreach($floss_name as $v){ 

   if(isset($v)){ // if $v is not empty run query and insert

      $query="INSERT INTO floss (floss_num , floss_qty , floss_name ) VALUES (NULL, '1', '$v')";
      $result=mysql_query($query);

   }//end if $v

};// end foreach

} // end if $_POST[];

?>

 

I have tried isset(), if(strlen($v) > 0)  if($v!=="") & if($v!==" ") and no matter what it inserts 1 blank entry. I type something like this in the textarea

-------

123     

         

456       

 

789

 

65477

-------

 

It will skip all empty items but 1 every time

 

Anyone got any ideas?

Thanks Paco... But same results sir.

 

I got it though, I did this

 

foreach($floss_num as $v){

$v=trim($v);  //white space was being added for some reason

if(strlen($v) > 0){

 

seems that an empty whitespace was the culprit

 

 

 

Archived

This topic is now archived and is closed to further replies.

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