PHP loops through the fields of the tables in the database and displays them to the front

Source: Internet
Author: User

Just in contact with PHP, you need to do a simple project.
Project Background
School teachers more or less need to count the students of various materials, in addition to such as questionnaires, gold data such as the third party platform, the school is temporarily not available for their own statistical platform.
The main functions are as follows:
1. The teacher creates the form according to the information he needs to be counted.
2. Students fill out the form and limit each student to only one piece of data in the form, and if the error is filled in, the last submission of the data will prevail.
3. The student fills in must real name, this system will call the easy class interface, obtains the student's real name information.
4. Teachers in the background can be exported students fill out the form content.
of which I wrote 2, 3,4.

The core of feature 2 is to iterate through all the fields of the tables in the database and display them to the front end . Database links.

$servername = "localhost";  
$username = "root"; 
$password = "xxxxxx";
$con = mysql_connect ($servername, $username, $password);
if (! $con)
{
            die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("Yibiao", $con);
Get table name from table ID
$tid = $_get["Tid"];
$sql = "Select ' Name ' from ' tablename ' where id=". $tid;//query id
$result = mysql_query ($sql);
while ($row = Mysql_fetch_array ($result))
{
  $table _name = $row [' name '];
}
Query the table structure and save the fields to the array
$sql _tbcolumns = "Show full COLUMNS from $table _name";//display array structure
$recolumns = mysql_query ($sql _tbcolumns);
$table _columns = Array (), and/or the fields in the table are stored in table_columns while
($row 1 = mysql_fetch_array ($recolumns))
{
  Array_push ($table _columns, $row 1[' Field ');
}
Read the length of the array to facilitate subsequent output
$columns _count = count ($table _columns);
Show the fields in the table to the front end and submit with Get
<!--form form submission--> <form action= "value.php" method= "get" > <?php $i = 0;
        while ($i < $columns _count) {echo ' <div class= ' zdy zwy1 ' > '; Echo ' <p> ' $table _columns[$i]. '
        </p> ';
        Echo ' <p> ';
        Echo ' <div class= ' sousuo1 ' > '; Echo ' <input name= ' TB '. $i. ' "type=" text "class=" biaodan-input1 "placeholder=" Please enter your '. $table _columns[$i].
        ' >  ';
        Echo ' </div> ';
        Echo ' </p> ';
        Echo ' </div> ';
    $i + +; ?> <!--hidden fields The total number of fields (columns_count) table name (table_name) user's real name (User_e_realname) to value.php--> Echo ' <?php UT name= "columns_count" type= "hidden" value= "". $columns _count. ' "
> '; Echo ' <input name= ' table_name ' type= ' hidden ' value= '. $table _name. ' "
> '; Echo ' <input name= ' user_e_realname ' type= ' hidden ' value= '. $user _e_realname. ' "      
> '; ?> <div class= "footerj1" > <button class= "btn btn-info" type= "submit" > Confirm Submit </button> &

 Lt;/div> </form>

Front-end display just like this ~ ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.