Jump to content

Inserting mysql data into html form


mattix

Recommended Posts

I am looking for a way to insert values from Mysql to HTML form. I searched the internet but all I can find is how to insert data from a form to Mysql. How can achieve this? Can anybody help me, please? thanks.

This is how my form looks like

<form id="form" name="form" method="post" action="">
               <label>                
                      <input type="checkbox" name="SelectAll" class="all" />All</label><label>
                      <input type="checkbox" name="Nissan" class="selector" />Nissan</label><label>
                      <input type="checkbox" name="Toyota" class="selector" />Toyota</label><label>
                      <input type="checkbox" name="Mercedes" class="selector" />Mercedes</label><label>
                      <input type="checkbox" name="BMW" class="selector" />BMW</label><label>
                      <input type="checkbox" name="Cadillac" class="selector" />Cadillac</label><label>
                      <input type="checkbox" name="Chevrolet" class="selector" />Chevrolet</label>
  </form> 

What I need is to insert the car brands from the mysql database automatically. So have rows of car brands on the data table and from these rows, the values will be fetched and inserted. 

This is what I came up with so far: 

 <?php
  include ("config.php");
  
    $sql = "SELECT ID, Brand1, Brand2, Brand3, Brand4, Brand5, Brand6 FROM BrandDb";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) 
     {
 
 
              <form id="form" name="form" method="post" action="">
              <label>                
                     <input type="checkbox" name="SelectAll" class="all" />All</label><label>
                     <input type="checkbox" name="Nissan" class="selector" />Nissan</label><label>
                     <input type="checkbox" name="Toyota" class="selector" />Toyota</label><label>
                     <input type="checkbox" name="Mercedes" class="selector" />Mercedes</label><label>
                     <input type="checkbox" name="BMW" class="selector" />BMW</label><label>
                     <input type="checkbox" name="Cadillac" class="selector" />Cadillac</label><label>
                     <input type="checkbox" name="Chevrolet" class="selector" />Chevrolet</label>
  </form> 
              
 
 
 
 } else { echo "0 results"; }
 $conn->close();
 ?>      

 

Link to comment
Share on other sites

Your first step is redesign the table. When you have fields named like that (Brand1, Brand2, ..., BrandN) you have got it wrong. The fields should be normalized so you have one brand name (and its id) per record.

+-------------------------+
|         brand           |
+----------+--------------+
| brand_id | brandname    |
+----------+--------------+
|    1     | Nissan       |
|    2     | Toyota       |
|    3     | Mercedes     |
|    4     | BMW          |
|    5     | Cadillac     |
|    6     | Chevrolet    |
+----------+--------------+

Then

SELECT brand_id, brandname FROM brand ORDER BY brandname

Now loop throught the results outputting the options. Note the name followed by "[]" and the checkbox values.

while fetch next row
    output "<input type='checkbox' name='brand[]' value='brand_id'>brandname<br>"
endwhile

Selected checkboxes are then sent as an array of brand_ids.

Link to comment
Share on other sites

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.