SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Multiple Records Showing NULL When Fields Not Used

    I'm running an experiment. First I've set up my form to upload multiple files and insert multiple records to a dbase table as such:

    HTML Code:
    <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" onsubmit="checkFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','');return document.MM_returnValue">
    <table width="700" align="center" id="registration">
    <tr valign="baseline">
    <td nowrap align="right"><strong>Upload File:</strong></td>
    <td valign="top"><input name="filename1" type="file" id="filename1" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','')" size="50" /></td>
    <td valign="top"><select name="category1" id="category1">
    <?php
    do {  
    ?>
    <option value="<?php echo $row_getCat['gc_id']?>"><?php echo $row_getCat['category']?></option>
    <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
    	  $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right"><strong>Upload File:</strong></td>
    <td valign="top"><input name="filename2" type="file" id="filename2" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','')" size="50" /></td>
    <td valign="top"><select name="category2" id="category2">
    <?php
    do {  
    ?>
    <option value="<?php echo $row_getCat['gc_id']?>"><?php echo $row_getCat['category']?></option>
    <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
    	  $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right"><strong>Upload File:</strong></td>
    <td valign="top"><input name="filename3" type="file" id="filename3" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','')" size="50" /></td>
    <td valign="top"><select name="category3" id="category3">
    <?php
    do {  
    ?>
    <option value="<?php echo $row_getCat['gc_id']?>"><?php echo $row_getCat['category']?></option>
    <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
    	  $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right"><strong>Upload File:</strong></td>
    <td valign="top"><input name="filename4" type="file" id="filename4" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','')" size="50" /></td>
    <td valign="top"><select name="category4" id="category4">
    <?php
    do {  
    ?>
    <option value="<?php echo $row_getCat['gc_id']?>"><?php echo $row_getCat['category']?></option>
    <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
    	  $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right"><strong>Upload File:</strong></td>
    <td valign="top"><input name="filename5" type="file" id="filename5" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,3000,'','','','','','')" size="50" /></td>
    <td valign="top"><select name="category5" id="category5">
    <?php
    do {  
    ?>
    <option value="<?php echo $row_getCat['gc_id']?>"><?php echo $row_getCat['category']?></option>
    <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
    	  $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right"> </td>
    <td colspan="2" valign="top"><input type="submit" value="Insert record"></td>
    </tr>
    </table>
    
    <input type="hidden" name="MM_insert" value="form1">
    </form>
    I have created multiple Insert Record Behaviors to post to the dbase table after someone clicks on the Insert button. Problem is, what if someone only wants to use 2 out of 5 fields on this form? Well, the rest of the three fields post as NULL, which I don't want. I don't want them to post at all! Does this require an IF/ELSE statement around the Insert Record Server Behavior?

    Currently I have this:

    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      
    $insertSQL sprintf("INSERT INTO gallery (gc_id, filename) VALUES (%s, %s)",
                           
    GetSQLValueString($_POST['category2'], "int"),
                           
    GetSQLValueString($_POST['filename2'], "text"));

      
    mysql_select_db($database_radmin$radmin);
      
    $Result1 mysql_query($insertSQL$radmin) or die(mysql_error());

      
    $insertGoTo "list.php";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
        
    $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$insertGoTo));

    I'd like to know how this can be modified for the logical function: "If there is something to upload, then post, otherwise, don't post anything".

    I know about looping, but I want to see if there is more than one way to skin a cat here.

    I appreciate your help.

  2. #2
    SitePoint Zealot jimmy85's Avatar
    Join Date
    Aug 2009
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about just putting in '' as values to the blanks in the form? They're not equal to null.

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question

    Wouldn't I then have a bunch of empty records in the dbase table?

  4. #4
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Yep...it just inserted multiple blank records in the table.

  5. #5
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Found the solution:
    Make sure that the database table field is set to NOT NULL.


Bookmarks

Posting Permissions

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