Select value isnt showing up...

Discussion in 'PHP' started by killaklown, Aug 24, 2006.

  1. #1
    Im trying to make it so that if they leave the drop down menu on Select A Category, it enters NULL into the mysql_query, but the value going into it (it is just blank)


    The select code is:

    
        $cat.='<select size="1" name="category" size="36" maxlength="255">';
        
    
     $sql = 'SELECT * FROM categories WHERE parent=0';
    $rs = mysql_query($sql);
    $cat.='<option value="NULL">Select A Category</option>';
    while($row = mysql_fetch_array($rs)){
      $ptitle = stripslashes($row[title]);
      $pid = stripslashes($row[id]);
    
     $cat.='<option value='.$pid.'>'.$ptitle.'</option>';
    $nsql = "SELECT * FROM categories WHERE parent='".$pid."'";
    $nrs = mysql_query($nsql);
    while($row = mysql_fetch_array($nrs)){
     $ctitle = stripslashes($row[title]);
     $cid = stripslashes($row[id]);
     $cat.='<option value='.$cid.'>--'.$ctitle.'</option>';
       }
    }  
    $cat.='</select>';
    
    PHP:
    The other option values work, just not the Select A Category one.

    The code for putting it into the database is:

    
    if($_POST['action'] == "edit") {
       $id = $_POST['id'];
       $ntitle = $_POST['title'];
       $nurl = $_POST['url'];
       $ncategory = $_POST['category'];
       $nimage = $_POST['image'];
       $nvalidate = $_POST['validate'];
       $ndescription = $_POST['description'];
       $ncount = $_POST['count'];
    
       
    if(($nimage=='')||($nimage=='http://')){
    $nimage="images/upload/noicon.gif";
    }
    
    $sql = mysql_query("UPDATE tutorials SET id='$id',title='$ntitle',url='$nurl',category='$ncategory',validate='$nvalidate',image='$nimage',count='$ncount',description='$ndescription' WHERE id='$id'") or die (mysql_error()); 
          
      echo "Tutorial Edited, Redirecting...";
             echo "<meta http-equiv=Refresh content=3;url=edittutorial.php?id=$id>";
    exit();
    
    }
    
    PHP:
     
    killaklown, Aug 24, 2006 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    Try this:
    replace the line
    $sql = mysql_query("UPDATE tutorials SET id='$id',title='$ntitle',url='$nurl',category='$ncategory',validate='$nvalidate',image='$nimage',count='$ncount',description='$ndescription' WHERE id='$id'") or die (mysql_error());
    PHP:
    with
    
    if ($ncategory !== 'NULL')
     $ncategory = "'".$ncategory."'";
    $sql = mysql_query("UPDATE tutorials SET id='$id',title='$ntitle',url='$nurl',category=$ncategory,validate='$nvalidate',image='$nimage',count='$ncount',description='$ndescription' WHERE id='$id'") or die (mysql_error());
    
    PHP:
     
    wmtips, Aug 24, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    tried that, but didnt work.
     
    killaklown, Aug 24, 2006 IP
  4. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #4
    can anyone help? Or if it is easier to automaticaly select the option which is in the database (ie the option has 1, 2, 3, 4, 5... and in the database it has 4 entered, so it will automaticaly select 4)
     
    killaklown, Aug 24, 2006 IP
  5. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you do me a favor?

    What kind of column is your category field in the database?

    Also, echo the SQL Statement and place it here.

    I'll see if I can help you out
     
    ip076, Aug 24, 2006 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Open PHPmyadmin,

    what is the field type for parent and is there a default value?

    Peace,
     
    Barti1987, Aug 25, 2006 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    I do not think you can convert STRING to NULL which is what you are doing.

    Set the value of the selection to "0"

    if ($_POST['selection'] == "0")
    {
    $updatevalue = NULL
    }
    else
    {
    $updatevalue = $_POST['selection'];
    }
     
    drewbe121212, Aug 25, 2006 IP
  8. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    what drew said!
     
    ip076, Aug 25, 2006 IP
  9. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #9
    I had the categories as a VarChar, changed to INT but it still doesnt work (tried the code that drew posted)
     
    killaklown, Aug 25, 2006 IP
  10. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #10
    this should do it:

    
    
    $get_parents = @mysql_query("SELECT `id`,`title` FROM `categories` 
    	WHERE `parent`='0' OR `parent` = '' OR `parent`='NULL'") or die (mysql_error);
    	
    $total_cats = @mysql_num_rows($get_parents);
    if($total_cats > 0){
    	$store = '<select size="1" name="category" size="36" maxlength="255">';
    	$store .='<option value="">Select A Category</option>';
    	while($each_cat = @mysql_fetch_array($get_parents)){
    		$store .='<option value='.$each_cat['id'].' style="bgcolor:#CCCCCC;">'.$each_cat['title'].'</option>';
    		$get_subs = @mysql_query("SELECT `id`,`title` FROM `categories` 
    				WHERE `parent`='$each_cat[id]'") or die (mysql_error);
    		$total_subcats = @mysql_num_rows($get_subs);		
    		if($total_subcats > 0){
    			$store .='<option value='.$each_cat['id'].'>--'.$each_cat['title'].'</option>';
    		}
    	
    	}
    	$store .= '</select>';
    } else {
    	$store = 'No listings yet!';
    }
    
    
    PHP:
    Peace,
     
    Barti1987, Aug 25, 2006 IP
  11. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #11
    um, that doesnt work, i dont see how it would put NULL into the row if the Select A Category is selected.

    The parent is either 0 (for a parent category) or the id of the parent category (if its a subcategory)
     
    killaklown, Aug 25, 2006 IP
  12. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #12
    Ok, i fixed it. Simplest thing ever :p

    just added: $category = $row["category"]; to get the category, and then i added:
    if ($_POST['category'] == "0"){
    $ncategory = $category;
    }else{
    $ncategory = $_POST['category'];
    }


    It works now, thanks.
     
    killaklown, Aug 25, 2006 IP