• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fill forms with db result!


I want to fill my form with data from the database.
The problem is if there is no row in the database then I got a error message you know!

<input type="text" name="head" value="<?=$query->head?>" size="50" />
<input type="text" name="title" value="<?=$query->email?>" size="50" />

This works, if there is things in the database, but if not, I got errors, how can I solve this?

Thanks //Webbhelp

how about

<?php if isset($query->head) echo $query->head ?>

or something along those lines... the syntax may not be correct of the top of my head (sorry still learning myself!) but you should get the idea of what is happening?

Usually it should not happen at all that your scripts
call an edit form and no db data is available ..

But you could loop through your db rows in your controller
and set vars like $data['form-title'] = $row->title; (whatever makes sence),
use an if statement to set the var to '' in case no row is returned.

Bur I think it would make more sence to check for results before
calling the edit form at all.


But I can't skip to load the form.
Because: If no row exists in the database then nothing will be written in the form, but if there is data then it should be data in the form!

Hi webbhelp,

One workaround I've done in the past is to have a method in the model that returns any empty object based on the table structure.
So, for example, when needed;

and the method itself...

function empty_row($table)

        $query = $this->db->query('SELECT * FROM '.$table.' LIMIT 1');

        foreach ($query->list_fields() as $field)
           $row->$field = '';

        return $row;


Of course you can refine this to select only certain fields etc but you get the general idea.

Hope this is of some help

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.