Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Insert a loop... insert only data

    Hi Guys,

    I am pretty new to PHP and need some help.

    I have a form with the following fields in my html form...

    Code:
    <form method="post" action="process.php" enctype="multipart/form-data"> 
    <select class="dropper" name="brand[]" onChange="getDescription('findDescription.php?brand='+this.value)">
    </select>
    <select class="dropper" name="descr[]" onChange="showUser(this.value);">
    </select>
    <select class="dropper" name="units[]" >
    </select>
    <select class="dropper" name="cases[]" >
    </select>
    <input TYPE="submit" name="submit" title="Add Post" value="Add Post"/>
    I have my process.php file that inserts the array into the my db with the following code...

    Code:
    <?
    
    $brand = $_POST['brand'];
    $descr = $_POST['descr'];
    $units = $_POST['units'];
    $cases = $_POST['cases'];
    
    $limit = count($brand);
    
    for($i=0;$i<$limit;$i++) {
        $brand[$i] = mysql_real_escape_string($brand[$i]);
        $descr[$i] = mysql_real_escape_string($descr[$i]);
        $units[$i] = mysql_real_escape_string($units[$i]);
        $cases[$i] = mysql_real_escape_string($cases[$i]);
        $query2 = "INSERT INTO sample_request_items (brand, descr, units, cases, srid) VALUES ('".$brand[$i]."','".$descr[$i]."','".$units[$i]."','".$cases[$i]."','".$srid."')";
    mysql_query($query2) or die(mysql_error());
        }
    ?>
    Which almost works great, the problem I am having is that it is inserting blank records... Does anyone know how I would be able to tell the script not to insert the blank records?

    Any help would be appreciated.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can check the values you're inserting and if they are empty, don't insert. You can use the empty() function...

    http://us3.php.net/manual/en/function.empty.php

    But keep in mind with empty(), values of "0" (zero) will return true. So if the value of zero is valid, you'll want to add that as an exception. Or, just check like this: if ($var == "") {}


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •