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:
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:
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)
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
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']; }
I had the categories as a VarChar, changed to INT but it still doesnt work (tried the code that drew posted)
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,
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)
Ok, i fixed it. Simplest thing ever 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.