Page 1 of 1

[Solved]How to create dynamic name for text box PHP?

Posted: Wed Sep 15, 2010 2:33 am
by VMthinker
Hi guys! I have a question regarding the creation of Auto Dynamic names for the text boxes within PHP. The scenario is like this:

* I have 2 tables in a MySWL database.
* The 2 tables (students/teachers) have different number of fields
* Teachers = 8 fields / Students = 5 fields
* A page for inserting new data into the tables is created.
* The text box appears but without any field names beside the text box.

So now there is a need to allow the "insert" page to auto generate the Field names found within the table to allow the naming of the various text box.

I think that by including the "mysql_fetch_field" function, it can be used to auto generate the field names needed for the PHP page.

Please kindly give some suggestions. Thanks!

Here are my codes:

Code: Select all

<?php

include "db_connect.php";

$name = mysql_query("SELECT * from users");

$property = mysql_fetch_field($result);

$result = mysql_query("SHOW COLUMNS FROM users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
    echo "$property: <input type=\"text\" name=\"{$row[0]}\" size=\"40\"
    maxlength=\"256\" /><br>";
    }
}
?>

Re: How to create dynamic name for text box PHP?

Posted: Wed Sep 15, 2010 3:40 am
by VMthinker
Looks like no one could answer the question. Anyway here's the code for those having the same PHP problem.

Code: Select all

<?php

include "db_connect.php";

$name = mysql_query("SELECT * from checkusers");

$property = mysql_fetch_field($name);

$result = mysql_query("SHOW COLUMNS FROM checkusers");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
        for ($i = 0; $i < mysql_num_fields($name); $i++)
        {
          $meta=mysql_field_name($name, $i);
          echo "$meta: <input type=\"text\" name=\"{$row['health_id']}\" size=\"40\" maxlength=\"256\" /><br>";
        }
        //echo "$new: <input type=\"text\" name=\"{$row[0]}\" size=\"40\" maxlength=\"256\" /><br>";
?>