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
https://forums.phpfreaks.com/topic/127405-solved-tag-and-php-help-plz/
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.

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>

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.