Hi,

I have scoured the net and can't find an answer so I wondered if you guys can help.

I have a combo box that selects data from a database, I can then add the selection of the combo box and other fields to another table, however when I do this with php the page refreshes and the value of the combo box changes back to the default order (where I want it to show the option I just added to the database)

I have tried the below code, theoretically I thought it might work but it doesn't (the leadProductCategory is the combo box selection that I add to the database - it is set in the php file where I add to the database).

$selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY .'".$_SESSION['leadProductCategory']."';
			$selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error());

		while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult))
                    { 
                         echo '<option value="'.$row1LeadProductCat['catname'].'">'.$row1LeadProductCat['catname'].'</option>'; 

       	        }

    I think this should work . . . Might need a little tweaking, though.

    while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) {
    
     echo '<option value="' . $row1LeadProductCat['catname'].'"';
     if( $row1LeadProductCat['catname'] == $_POST['select_name'] ) { //change 'select_name' to the value of your <select name="
          echo ' selected="selected"';
     }
     echo '>' . $row1LeadProductCat['catname'] . '</option>';
    }
    
      Pikachu2000;10948399 wrote:

      I think this should work . . . Might need a little tweaking, though.

      while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) {
      
       echo '<option value="' . $row1LeadProductCat['catname'].'"';
       if( $row1LeadProductCat['catname'] == $_POST['select_name'] ) { //change 'select_name' to the value of your <select name="
            echo ' selected="selected"';
       }
       echo '>' . $row1LeadProductCat['catname'] . '</option>';
      }
      

      hi, thanks, I haven't got this working yet, i'll keep going!

        use Pikachu2000's suggestion instead.

        In you original code you had SQL error, plus in your PHP you had typo error...
        Where is the error message in your question?

        <select name="cat">
        <?php
        $selectleadpr = "SELECT * FROM category where userid = " . $_SESSION['userid'] . " ORDER BY " . $_SESSION['leadProductCategory'];
        
        $default = isset($_POST['cat'])?$_POST['cat']:'';
        while ($row = mysql_fetch_assoc($selectleadpr)) {
            $selected = $default == $row['catname']?' selected="selected"':'';
            echo '<option value="' . $row['catname'] . '" ' . $selected . '>' . $row['catname'] . '</option>';
        } 
        
        ?></select>

        with mysql_fetch_array you'r wasting your servers resources, because its retrieving associated and numerical indexes in your $row array, where numerical indexes are redundant.

        You should use shorter variable names, and boycott uppercase variable names which is growing the chance to make misspells.

          shure2;10948404 wrote:

          hi, thanks, I haven't got this working yet, i'll keep going!

          I solved it by putting the value into a session variable when its added to the database and then used the following code:

                  <select name="leadprodcategory">
          		<?php
          			$selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY catname";
          			$selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error());
          
                              while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) {
          
           echo '<option value="' . $row1LeadProductCat['catname'].'"';
           if( $row1LeadProductCat['catname'] == $_SESSION['leadProductCategory'] ) { //change 'select_name' to the value of your <select name="
                echo ' selected="selected"';
           }
           echo '>' . $row1LeadProductCat['catname'] . '</option>';
          }
          
          
          	?>
          	</select>
          

          thanks for the help again! this forum rocks!

            djjjozsi;10948426 wrote:

            use Pikachu2000's suggestion instead.

            In you original code you had SQL error, plus in your PHP you had typo error...
            Where is the error message in your question?

            <select name="cat">
            <?php
            $selectleadpr = "SELECT * FROM category where userid = " . $_SESSION['userid'] . " ORDER BY " . $_SESSION['leadProductCategory'];
            
            $default = isset($_POST['cat'])?$_POST['cat']:'';
            while ($row = mysql_fetch_assoc($selectleadpr)) {
                $selected = $default == $row['catname']?' selected="selected"':'';
                echo '<option value="' . $row['catname'] . '" ' . $selected . '>' . $row['catname'] . '</option>';
            } 
            
            ?></select>

            with mysql_fetch_array you'r wasting your servers resources, because its retrieving associated and numerical indexes in your $row array, where numerical indexes are redundant.

            You should use shorter variable names, and boycott uppercase variable names which is growing the chance to make misspells.

            thank you for the advice,

            thanks for the code you provided, I tried it but the listbox is not populated with any values when I refreshed the page, and IE doesn't show any errors 🙁

            I did get it working the way I said above, but am keen to get it working properly and now I need to do the same with the below code:

            <select name="leadid" onchange="showLeadName()">
            		<?php
            			$selectleadq = "SELECT * FROM lead where userid = '".$_SESSION['userid']."' ORDER BY leadname";
            			$selectleadr = mysql_query($selectleadq) or die(mysql_error());
            
            
            		while($leadrow1=mysql_fetch_array($selectleadr))
                                { ?>
                                   <?php echo '<option value="'.$leadrow1['leadid'].'">'.$leadrow1['leadname'].'</option>'; ?>
            
                   	<?php   }
            	?>
            
            	</select>
            

            As I am using numerical indexes in my $row array is it appropriate to use mysql_fetch_array?

              shure2 wrote:

              As I am using numerical indexes in my $row array is it appropriate to use mysql_fetch_array?

              But you aren't using numerical indexes. This:

              $leadrow1['leadid']

              does not use a numerical index. In other words, "leadid" isn't a number (that I know of, anyway).

                bradgrafelman;10948545 wrote:

                But you aren't using numerical indexes. This:

                $leadrow1['leadid']

                does not use a numerical index. In other words, "leadid" isn't a number (that I know of, anyway).

                the leadid is an int which is a primary key that auto-increments every time I add a record, i'm a noobie but i thought that was a numerical index, sorry if im wrong.

                  You're confusing the array index with that array index's value. The array index, as far as PHP concerned, is the name (or number) that you're using to access an item in an array.

                  Since $leadrow1['leadid'] is a lot easier to understand than $leadrow1[0], there's no reason to switch to using numerical indexes. Thus, since you're not using them, djjjozsi suggested you use something like [man]mysql_fetch_assoc/man which doesn't duplicate each column by creating an array with numerical and string indexes for all columns.

                    bradgrafelman;10948562 wrote:

                    You're confusing the array index with that array index's value. The array index, as far as PHP concerned, is the name (or number) that you're using to access an item in an array.

                    Since $leadrow1['leadid'] is a lot easier to understand than $leadrow1[0], there's no reason to switch to using numerical indexes. Thus, since you're not using them, djjjozsi suggested you use something like [man]mysql_fetch_assoc/man which doesn't duplicate each column by creating an array with numerical and string indexes for all columns.

                    ok I see, thank you for the explanation.

                    I have changed the code to below, can anyone help modify it so its like the category listbox before where it shows the value that was selected previously?

                    <select name="leadid" onchange="showLeadName()">
                    		<?php
                    			$selectleadq = "SELECT * FROM lead where userid = '".$_SESSION['userid']."' ORDER BY leadname";
                    
                    
                    
                    		while($leadrow1 = mysql_fetch_assoc($selectleadq))
                                        { ?>
                                           <?php echo '<option value="'.$leadrow1['leadid'].'">'.$leadrow1['leadname'].'</option>'; ?>
                    
                           	<?php   }
                    	?>
                    
                    
                    	</select>
                    
                    

                      The logic is the same... on each iteration in the while() loop, check if the current row being outputted matches the value of the POST'ed selection. If it does, output the necessary HTML to make that option be selected by default.

                        Write a Reply...