ltdev
ltdev

Reputation: 4497

Fetch data in order to edit on form

I have created this table:

Settings
-----------
id (PK)
name
value (default: NULL)
catogory // not sure if I actually will need it

and inserted some data, eg:

1, description, NULL, meta
2, keywords, NULL, meta
3, color, NULL, shirts
4, size, NULL, shirts

What I want to do for example is to fetch on my page all data that belong in meta category, so that I can edit the value collumn. So basicly my form will have to look like this:

<form>
    <input value=" (the value if keywords here) ">
    <textarea>(the value if description here)</textarea>
</form>

then on another page to edit the settings for shirts category etc etc.

This is my query

SELECT * FROM (`settings`) WHERE `category` =  'meta' ORDER BY `id`

but I cant figure out how to put each value on the specific field, without looping the data

Upvotes: 0

Views: 213

Answers (1)

CodeBird
CodeBird

Reputation: 3858

This will work as you want, but if you tell us what exactly you're trying to do, there could be a better method.

<?php 
$query=mysqli_query($conn, "SELECT * FROM (`settings`) WHERE `category` =  'meta' ORDER BY `id`");
?>
<form>
<?php 
while($array=mysqli_fetch_assoc($query)){
      if($array['name']=='keywords'){ ?>
          <input value="<?php echo stripslashes($array['name']);?>">
      <?php } elseif($array['name']=='description'){ ?>
          <textarea><?php echo stripslashes($array['value']);?></textarea>
      <?php } ?>
<?php }?>
</form>

Upvotes: 1

Related Questions