Darkwoods Posted October 7, 2008 Share Posted October 7, 2008 im trying on my edit page to put a select drop down box geting the value and data from the databse i tried this code below but not working and i knew that it wont work anyone who can help plz <select name="cat"> <?php $sql = 'SELECT DISTINCT `catname` from `categories`'; $result = @mysql_query($sql,$connect); while ($row = @mysql_fetch_assoc($result)) { echo '<option value="'.$row['cat'].'" selected="$cat">'.$row['catname'].'</option>'; } ?> here is the whole page code <?php include "inc/header.php"; $id = $_GET['id']; if(isset($_POST['submit'])) { //global variables include "inc/variables.php"; $result = mysql_query("UPDATE contents SET title_en='$title_en', title_ar='$title_ar', en='$en', ar='$ar', cat='$cat' WHERE id='$id' ",$connect); echo "<b>Your post have been added successfully"; echo "<meta http-equiv=Refresh content=2;url=index.php>"; } elseif($id) { $result = mysql_query("SELECT * FROM contents WHERE id='$id' ",$connect); while($row = mysql_fetch_assoc($result)) { $title_ar = $row["title_ar"]; $title_en = $row["title_en"]; $en = $row["en"]; $ar= $row["ar"]; $cat= $row["cat"]; ?> <br> <h3>::Edit News</h3> <form method="post" action="<?php echo $PHP_SELF ?>"> <input type="hidden" name="id" value="<?php echo $row['id']?>"> <table border="0" cellspacing="4" cellpadding="4"> <tr> <td>Select Category:</td> <td colspan="2"> <select name="cat"> <?php $sql = 'SELECT DISTINCT `catname` from `categories`'; $result = @mysql_query($sql,$connect); while ($row = @mysql_fetch_assoc($result)) { echo '<option value="'.$row['cat'].'" selected="$cat">'.$row['catname'].'</option>'; } ?> </select></td> </tr> <tr> <td>Title:</td> <td class="title_en1"><input name="title_en" size="40" maxlength="255" value="<?php echo $title_en; ?>"></td> <td class="title_ar1"><input name="title_ar" size="40" maxlength="255" value="<?php echo $title_ar; ?>"></td> </tr> <tr> <td>Content:</td> <td class="title_en1"><textarea name="en" rows="7" cols="30"><?php echo $en; ?></textarea></td> <td class="title_ar1"><textarea name="ar" rows="7" cols="30"><?php echo $ar; ?></textarea></td> </tr> <tr> <td colspan="3" class="submit"><input type="submit" name="submit" value="Submit"></td> </tr> </table> <?php } //end of while loop }//end else include "inc/footer.php"; ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 Instead of suppressing the errors, show them: <select name="cat"> <?php $sql = 'SELECT DISTINCT `catname` from `categories`'; $result = mysql_query($sql,$connect) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo '<option value="'.$row['cat'].'" selected="$cat">'.$row['catname'].'</option>'; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 $selected = ($cat == $row['id']) ? ' selected="selected"' : ''; echo '<option value="'.$row['cat'].'"'.$selected.'>'.$row['catname'].'</option>'; Quote Link to comment Share on other sites More sharing options...
Darkwoods Posted October 7, 2008 Author Share Posted October 7, 2008 thanks guys... but the value is not working Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 Using the piece of code I supplied, are you getting any errors? If not, define "not working"... What happens? Orio. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 7, 2008 Share Posted October 7, 2008 instead of using: mysql_fetch_assoc use mysql_fetch_array Quote Link to comment Share on other sites More sharing options...
Darkwoods Posted October 7, 2008 Author Share Posted October 7, 2008 Using the piece of code I supplied, are you getting any errors? If not, define "not working"... What happens? Orio. hi thanks.. no im not getting error the <option value="" is just empty and i would need the value to send it to the database instead of using: mysql_fetch_assoc use mysql_fetch_array same problem value is empty here is what im getting in HTML ass you can see the value is empty <select name="cat"> <option value="" selected="selected">Home</option><option value="" selected="selected">About Us</option> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 $sql = 'SELECT DISTINCT `cat`, `catname` from `categories`'; Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 I think you have a mistake in your column name: Try: echo '<option value="'.$row['catname'].'" selected="$cat">'.$row['catname'].'</option>'; If that doesn't work maybe you didn't fetch all the data needed from the table. Orio. Quote Link to comment Share on other sites More sharing options...
Darkwoods Posted October 7, 2008 Author Share Posted October 7, 2008 thank you again this code made it work <select name="cat"> <?php $sql = 'SELECT DISTINCT `id`, `catname` from `categories`'; $result = @mysql_query($sql,$connect); while ($row = @mysql_fetch_array($result)) { $selected = ($cat == $row['id']) ? ' selected="selected"' : ''; echo '<option value="'.$row['id'].'"'.$selected.'>'.$row['catname'].'</option>'; } ?> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 Not a problem I should've known that the column name was 'id' and not 'cat'. I'm tired Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.