0
votes

I have three tables

  • City: city_id, city_name
  • State: State_id, State_name
  • News: City_id, State_id, Headline, Story, Author etc.

I am Displaying data in textbox and listbox on edit button click Of Selected ID.

Data is displaying in listbox properly of selected id but when i click save button without change of listbox value it goes 0 in database.

If I don't change the listbox value and click save then it inserting 0 from listbox.

<?php 
$data = 0;
if(isset($_GET['edit']))
{
$id = clean($_GET['edit']);
mysql_set_charset('utf8'); 
$sql="SELECT city_name,state_name,category_name,headline,author,story,source,photo,date from news left join
 city on news.city_id=city.city_id left join state on news.state_id=state.state_id left join category on news.cat_id=category.category_id where id = '$id'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
}
?>

<?php 
$cat = $Admin->select($Admin->cat_table,'','','');  
?>

<select name="cat_id" class="select"  required="">
    <option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>"><?php if(isset($_GET['edit'])){echo $data['category_name'];}?></option>      
    <?php                  
    foreach($cat as $load_category)
    {                       
    ?>  
    <option value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
    <?php }?>
</select>

<?php           
        $errors = array();      
        $Admin = new admins;                
        if(isset($_POST['save']))
        {   
                $table = $Admin->news_table;
                    if(isset ($_GET['edit']))           
                    {                   
                            $id = clean($_GET['edit']);
                            $cond = "id = '$id'";                       
                            if($Admin->save_advance($table,$_POST,'',$cond))
                            {
                                $_SESSION['message'] = "News Updated Successfully";                 
                                header("Location:add_news.php");
                                exit;       
                            }               

                    }                                               

        }
?>  

What am I doing wrong?

1
can you provide the generated HTML and the action on save button?theLibertine
if(isset ($_GET['edit'])) { $id = clean($_GET['edit']); $cond = "id = '$id'"; if($Admin->save_advance($table,$_POST,'',$cond)) { $_SESSION['message'] = "News Updated Successfully"; header("Location:add_news.php"); exit; } }suresh bajpai
please don't write it in the comments, just edit your question. Then let me understand, the save button is updating the table or not?theLibertine
What's this? $id = clean($_GET['edit']); What clean does?vaso123

1 Answers

0
votes

Tell me if I'm wrong but this line

<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>">
    <?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option> 

seems strange to me, it sets the category name as value for first option in the select. Basically, if you don't change the listbox, you are trying to update a row where the category_id is a category_name. This should be:

<option value="0">
    <?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option> 

But if you have to update something you can't do that without a value. So add some kind of validation that forbids to click the save button without setting a value in the select, or just do something like this (adds a default category to the listbox)

<select name="cat_id" class="select" required>  
    <?php   
    $counter = 0;                        
    foreach($cat as $load_category){ ?>  
    <option <?php echo ($counter == 0) ? 'selected' : ''; ?> value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
    <?php
    //increments counter
    $counter++; }?>
</select>