Link to home
Start Free TrialLog in
Avatar of dprasad
dprasad

asked on

insert statement not evaluating

I'm trying to run an insert then update that newly inserted row. I'm echoing the sql statements, the insert isn't getting printed out so I'm pretty sure it's not getting evaulated. The update is run fine, if the associated keyword id is in the db (not the usual case)l. I only want to do the initial insert for the first element in the keywordelement array

if(isset($_POST['keywordparam'])){
    for($no=0;$no<count($_POST['keywordparam']);$no++){
        $keywordelement = $_POST['keywordparam'][$no];
                  
                        $seltype = $_POST['selecttype'];
                  $sel = $_POST['select1'];
                        
                        if ($no == 1) {
                        $keywordelementfirst = $_POST['keywordparam'][0];
                        
                        if ($seltype == 2 ) {
                        $selecttype = 'theme';
                        }
                        else if ($seltype == 3 ) {
                        $selectttype = 'place';
                        }
                        
                        if ($sel == 3) {
                        $select1 = 'SDS';
                        echo select1;
                        }
                        $updateq = "insert into k3gisdcp.keywords (keyword_id, keyword, author, type) values ('$findkwresult', '$keywordelementfirst', '$select1', '$selecttype')";
                      echo $updateq;
                        $s2 = OCIParse($c1, $updateq) or die("update error : $updateq");
                        OCIExecute($s2);
                        $committed2 = ocicommit($c1);
      }
      //Add selected keywords to newly added record
      
      
      //update k3gisdcp.keywords set keyword = keyword ||  'keyword: " . $keywordelement . "' where keyword_id = '{$findkwresult[$no]}'";
      //$updatek = "update k3gisdcp.keywords set keyword = keyword || 'keyword: . $keywordelement . "' where keyword_id = '$findkwresult'";
      $updatek = "update k3gisdcp.keywords set keyword = keyword ||  '" . $keywordelement . ",' where keyword_id = '$findkwresult'";
    $s2 = OCIParse($c1, $updatek) or die("update error : $updatek");
                  OCIExecute($s2);
                  echo $updatek;
                  $committed2 = ocicommit($c1);
   }
  }

}
?>
//here's my html

<form name="form1" method="post" action="">
  <table width="694" border="0">
    <tr>
      <td width="121">Current type: <?php $c1 = ocilogon("K3GISDCP", "K3GISDCP", "GIS");
$query = "select k.type as TYPE, k.keyword_id, dckw.keywords_id, dckw.dc_id from keywords k, dc_kw dckw where k.keyword_id = dckw.keywords_id and dckw.keywords_id ='$id1'";
$statement = OCIParse ($c1, $query);
OCIExecute ($statement);
       while (OCIFetchInto ($statement, $row, OCI_ASSOC)) {
        
         ?><b><?php echo $row['TYPE'];?></b> <?php
            } ?>
</td>
      <td width="256"><select name="selecttype" onChange='OnChangeFunc();'>
        <option value="1">Choose</option>
            <option value="2">theme</option>
        <option value="3">place</option>
      </select>
      Select the type for this item.</td>
      <td width="303">&nbsp;</td>
    </tr>
    <tr>
      <td>Current authority: <?php $c1 = ocilogon("K3GISDCP", "K3GISDCP", "GIS");
$query = "select k.author as AUTHOR, k.keyword_id, dckw.keywords_id, dckw.dc_id from keywords k, dc_kw dckw where k.keyword_id = dckw.keywords_id and dckw.keywords_id ='$id1'";
$statement = OCIParse ($c1, $query);
OCIExecute ($statement);
       while (OCIFetchInto ($statement, $row, OCI_ASSOC)) {
        
         ?><b><?php echo $row['AUTHOR']; ?></b><?php
            } ?></td>
      <td><select name="select1" onChange='OnChangeFunc();'>
        <option value="1">Choose</option>
            <option value="2">blank</option>
        <option value="3">SDS</option>
            <option value="4">FIPS</option>
      </select>
Select the authority for this item.</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Current Keywords:<br><?php $c1 = ocilogon("K3GISDCP", "K3GISDCP", "GIS");
$query = "select k.keyword as KEYWORD, k.keyword_id, dckw.keywords_id, dckw.dc_id from keywords k, dc_kw dckw where k.keyword_id = dckw.keywords_id and dckw.keywords_id ='$id1'";
$statement = OCIParse ($c1, $query);
OCIExecute ($statement);
       while (OCIFetchInto ($statement, $row, OCI_ASSOC)) {
        
         ?><b><?php echo $row['KEYWORD']; ?></b><?php } ?> </td>
      <td align="center" valign="top"><div align="left">
        <select name="keywordparam[]" size="25" multiple>
            <option value="1">auditory</option>
            <option value="2">boundary</option>
            <option value="3">buildings</option>
            <option value="4">cadastre</option>
            <option value="5">climate</option>
            <option value="6">common</option>
            <option value="7">communications</option>
            <option value="8">cultural</option>
            <option value="9">demographics</option>
            <option value="10">ecology</option>
            <option value="11">environmental_hazards</option>
            <option value="12">flora</option>
            <option value="13">future_projects</option>
            <option value="14">geodetic</option>
            <option value="15">geology</option>
            <option value="16">hydrography</option>
            <option value="17">improvement</option>
            <option value="18">land_status</option>
            <option value="19">landform </option>
            <option value="20">military_operations</option>
            <option value="21">olfactory</option>
            <option value="22">soil</option>
            <option value="23">transportation</option>
            <option value="24">utilities</option>
            <option value="25">visual</option>
        </select>
      </div></td>
      <td align="center" valign="middle">
        <div align="left">
          <p>Add keyword(s) to this item.<br>
            (<span class="style1">Hold down control key while clcicking to select multiple keywords) </span></p>
          <p>&nbsp;</p>
        </div></td>
    </tr>
  </table>
  <p>
    <input type="submit" name="submit" value="submit">
</p>
  <p>&nbsp;</p>
  <p>&nbsp;    </p>
  <p>&nbsp;    </p>
  <p>&nbsp;</p>
</form>
</body>
TIA

Dinesh
Avatar of Batalf
Batalf
Flag of United States of America image

What's the purpose of these lines?

if ($no == 1) {
                    $keywordelementfirst = $_POST['keywordparam'][0];

Shouldn't the ending bracket be right after it:

if ($no == 1) {
                    $keywordelementfirst = $_POST['keywordparam'][0];
}

and not at the end of the update insert block?




Avatar of dprasad
dprasad

ASKER

hmm yes you are right, but when I end that if like that, the query tries to insert a null for the $keywordelementfirst argument

insert into k3gisdcp.keywords (keyword_id, keyword, author, type) values ('5', '', 'SDS', 'theme')
Warning: ociexecute(): OCIStmtExecute: ORA-01400: cannot insert NULL into ("K3GISDCP"."KEYWORDS"."KEYWORD") in /var/www/html/dc/dc/keywordedit5.php on line 107
Do you need the $keywordelementfirst at all?

Maybe it could be like this:

if(isset($_POST['keywordparam'])){
      $seltype = $_POST['selecttype'];      
      $sel = $_POST['select1'];
    if ($seltype == 2 ) {
        $selecttype = 'theme';
    }
    else if ($seltype == 3 ) {
        $selectttype = 'place';
    }
   
    if ($sel == 3) {
        $select1 = 'SDS';
        echo $select1;
    }
                  
    for($no=0;$no<count($_POST['keywordparam']);$no++){
        $keywordelement = $_POST['keywordparam'][$no];
       

            $updateq = "insert into k3gisdcp.keywords (keyword_id, keyword, author, type) values ('$findkwresult', '$keywordelement', '$select1', '$selecttype')";
           echo $updateq."<br>";
                    $s2 = OCIParse($c1, $updateq) or die("update error : $updateq");
                    OCIExecute($s2);
                    $committed2 = ocicommit($c1);
     }
     //Add selected keywords to newly added record
}
     
Avatar of dprasad

ASKER

Well the thing is, I only want to insert this record once, not $no<count($_POST['keywordparam']) times..
That's why I was trying to access only the first element of the array. I know its weird, the DB is denoarmalized and will have a some repeating data in it.
Avatar of dprasad

ASKER

the update query handles multiple selections from a multi select html box
Maybe you could drop the for-loop and just have:


        $keywordelement = $_POST['keywordparam'][0];
            $updateq = "insert into k3gisdcp.keywords (keyword_id, keyword, author, type) values ('$findkwresult', '$keywordelement', '$select1', '$selecttype')";
           echo $updateq."<br>";
                    $s2 = OCIParse($c1, $updateq) or die("update error : $updateq");
                    OCIExecute($s2);
                    $committed2 = ocicommit($c1);
     
Avatar of dprasad

ASKER

ok, no that doesnt seem to work
Avatar of dprasad

ASKER

I may just try to do the insert the two drop down select fields and insert a dummy value for the keyword. then, delete that value and do the update from another page
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dprasad

ASKER

Ok, I changed another page and made this an update instead of an insert, seems to work. god I wish they would let me alter the DB to add more tables...they need to read a book on DB normalization and apply it! grr