checkbox - Get Table Column Names from MySQLi and Using PHP create Check Boxes -


have not programmed in on 20 years. trying use php dynamically create check boxes getting column names database table (fruit). have researched hundreds of postings examples unsuccessfully. overview of doing:

step a: (1) using php, name each column (field) table (fruit) in database. (2) each column (field) name, dynamically create checkbox column name beside in website form viewable members. (3) css formatting created in later coding efforts. (4) php code change table name, reused numerous times same function several other web pages (i.e. tables – vegetables, melons, etc….)

step b: (1) members select (click on) 1 checkboxes generated in step a. (2) members select (click on) submit causing each selected checkbox item written new row (record), appearing in correct column in table (fruit).

step c: (1) members able search database see fruit items other members have posted in table (fruit). (2) search feature incorporated in later coding effort web site.

below code sort of works, fails meet goal because each table require knowing number of columns, coding page number of columns, , if table changes have mod code page.

    <?php      $link = mysql_connect('localhost', 'myneighborhood01', 'tsc-100');     if (!$link) {         die('could not connect mysql server: ' . mysql_error());     }     echo "connected data base <br>";      $dbname = 'mydatabase';     $db_selected = mysql_select_db($dbname, $link);     if (!$db_selected) {         die("could not set $dbname: " . mysql_error());     }     $res = mysql_query('select * users', $link);      echo mysql_field_name($res, 0) . "\n";     echo mysql_field_name($res, 1);     echo mysql_field_name($res, 2);     echo mysql_field_name($res, 3);     echo mysql_field_name($res, 4);     echo mysql_field_name($res, 5);     echo mysql_field_name($res, 6);     ?> 

the following code closer trying do.

    <?php     $servername = "localhost";     $username = "username";     $password = "password";     $dbname = "mydb";      // create connection     $conn = new mysqli($servername, $username, $password, $dbname);     // check connection     if ($conn->connect_error) {         die("connection failed: " . $conn->connect_error);     }      $sql = "select * table_name ";     $result = $conn->query($sql);      if ($result->num_columns > 0) {         // output data of each column         while($column = $result->fetch_assoc()) {             echo "" . $column[]. "<br>";         }     } else {         echo "0 results";     }     $conn->close();     ?>  


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -