mattix Posted April 13, 2018 Share Posted April 13, 2018 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(); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2018 Share Posted April 13, 2018 (edited) 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. Edited April 13, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
mattix Posted April 13, 2018 Author Share Posted April 13, 2018 Thanks, Barand. I will try your suggestion. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.