如何在php的网页中显示整个数据库存储的记录?

时间:2015-12-30 10:05:34

标签: php

Form.html

<html>
    <head><title>PHP Form</title>
            </head>

    <body bgcolor = "#b3ffff">

        <div>
            <h1 align = "center" border = "5">Dream media solution</h1>
        </div><br>
            <div>
        <form method = "post" action = "welcome.php">

            <table width = "640" align = "center" border = "5" border-color = "red">





            <tr>
                <th>Name:</th>
                <th><input type = "text" name = "uname" placeholder = "Enter ur Name" size="28"></th>
            </tr>
            <tr>
                <th>Email:</th>
                <th><input type = "email" name = "uemail" placeholder = "Enter ur Email" size="28"></th>
                </tr>
                <tr>
                <th>Password:</th>
                <th><input type = "password" name = "upass" placeholder = "Enter ur Password" size="28"></th>
                </tr>
                <tr>
                <th>Address:</th>
                <th><textarea size="26" name="address" rows="3" cols="26"></textarea></th>
                </tr>
                <tr>
                <th>Gender:</th>
                <th><input type="radio" name="gender" value="female">Female
                        <input type="radio" name="gender" value="male">Male</th>    
                </tr>
                <tr>
                <th>Course:</th>
                        <th><input type="checkbox" name="course[]" value="PHP">PHP
                        <input type="checkbox" name="course[]" value="Java">Java
                        <input type="checkbox" name="course[]" value=".NET">.NET
                        <input type="checkbox" name="course[]" value="Web Designing">Web Designing</th>
                        </tr>
                    <tr>
                        <td colspan = "2" align = "center"><input type = "submit" value  = "login" name = "subname">
                            <input type = "reset" value = "clear"><td>
                    </tr>

            </table>

        </form>
        </div>
    </body>
    </html>

的welcome.php

<body bgcolor = "pink">
<font size = "6" color = "yellow">
    <?php

    if (isset($_POST['subname'])) 
    {
        $counter = 1;
        $name = $_POST['uname'];
        $email = $_POST['uemail'];
        $pass = $_POST['upass'];
        $gender = $_POST['gender'];
         $cou = implode(",", $_POST['course']);

         $address = $_POST['address'];
         //Database connection
        $con = mysqli_connect("localhost", "root", "");
        // create databse
        mysqli_query($con, "CREATE DATABASE sms");
        //select database
        mysqli_select_db($con, "sms");

        //Create table
        $ct = "CREATE TABLE student1(sno INTEGER(3) AUTO_INCREMENT PRIMARY KEY, 
        name VARCHAR(20), email VARCHAR(20), password VARCHAR(20), address VARCHAR(50), gender VARCHAR(30), course VARCHAR(30))";
        //Excute table through database
        mysqli_query($con,$ct);
        //Insert records into table through database
        $res = mysqli_query($con, "INSERT INTO student1 VALUES('', '$name', '$email', '$pass','$address', '$gender', '$cou')");
        echo "<table width = '640' align = 'center' border = '5' bordercolor = 'red' bgcolor='green' cellpadding = '12'>";
        $result  = mysqli_query($con, "SELECT * FROM student1");
        print_r ($result);
        echo "<tr>";
        echo "<th>Sno</th>";
        echo "<th>Name</th>";
        echo "<th>Email</th>";
        echo "<th>Password</th>";
        echo "<th>Address</th>";
        echo "<th>Gender</th>";
        echo "<th>Course</th>";
        echo "<tr>";
        echo "<th>".$counter."</th>";
        echo "<th>".$_POST['uname']."</th>";
        echo "<th>".$_POST['uemail']."</th>";
        echo "<th>".$_POST['upass']."</th>";
        echo "<th>".$_POST['address']."</th>";
        echo "<th>".$_POST['gender']."</th>";
        echo "<th>".$cou."</th>";

        echo "</tr>";
        echo "</table>";
        if ($res) {
            $msg = "Student Details uploaded successfully";
        }

        else
        {
            $msg = "There is Error in Details please checkout";
        }






            }
    ?>
</font>

我的数据库名称是短信,表名是student1。

这里我只在网页上显示一条记录。

但是我需要在数据库中有多少记录我将它循环并显示在网页中,如何才能帮助我。

3 个答案:

答案 0 :(得分:1)

如果您想显示所有数据库记录,请尝试以下代码:

    echo "<table width = '640' align = 'center' border = '5' bordercolor = 'red' bgcolor='green' cellpadding = '12'>";
    $result  = mysqli_query($con, "SELECT * FROM student1");


    echo "<tr>";
    echo "<th>Sno</th>";
    echo "<th>Name</th>";
    echo "<th>Email</th>";
    echo "<th>Password</th>";
    echo "<th>Address</th>";
    echo "<th>Gender</th>";
    echo "<th>Course</th>";
    echo "</tr>";

    $i = 1;
    while($row = mysql_fetch_assoc($result)){        
            echo "<tr>";
            echo "<th>".$i."</th>";
            echo "<th>".$row['name']."</th>";
            echo "<th>".$row['email']."</th>";
            echo "<th>".$row['password']."</th>";
            echo "<th>".$row['address']."</th>";
            echo "<th>".$row['gender']."</th>";
            echo "<th>".$row['course']."</th>";
            echo "</tr>";
    $i++;
    }
    echo "</tr>";
    echo "</table>";

答案 1 :(得分:0)

使用while {} ..

  <?php
 $sql=$con,"SELECT * FROM student1";
$i = 1;
 $select = mysql_query($sql);
 while($row = mysql_fetch_assoc($select)){ 

 // Set your Values

 $i++;

    }       ?>

OR

参考W3Schools

答案 2 :(得分:-1)

通过添加循环或动态循环来显示内容,在您的显示(表单html)页面上进行更改...使用此代码并根据它进行更改..

//创建连接