Jump to content

[SOLVED] <select> tag and php help plz


Darkwoods

Recommended Posts

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";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
} ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.