html表使用php从mysql中获取行时获取额外的空白值

时间:2014-11-21 04:38:55

标签: php mysql

  1. 我有一个welcome.php文件,我需要从页面上的mysql数据库中获取记录到html表。
  2. 获取记录但在每个列字段后面放置一个空白字段。例如如果有6列。然后它显示12个字段。每个字段后有6个实际字段和一个空白字段。所以,html表结构enter code here也令人不安。
  3. 我使用了mysqli_fetch_array并设置了一个while循环来获取20条记录。我不确定在获取记录时是否犯了错误或者html表本身存在问题。请找到下面的代码,让我知道我在做什么错。 感谢。

     <?php 
    //start a session
    session_start();
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    
    <style>
    nav ul{
        margin:0;padding:0;
    
    }
    
    table,th,td{
        border: 1px solid black;
        border-collapse:collapse;
        text-align:left;
        table-layout:auto;
    
    
    }
    
    
    nav ul li{
    
        display:list-item;float:left;margin:5px;
    }
    
    a:link    {color:#000000; background-color:transparent; text-decoration:underline}
    a:visited {color:#000000; background-color:transparent; text-decoration:none}
    a:hover   {color:#ff0000; background-color:transparent; text-decoration:underline}
    a:active  {color:#ff0000; background-color:transparent; text-decoration:underline}
    
    
    
    </style>
    </head>
    <body style="background-color: white;">
    
    <?php
    //Define variables 
    $company=$ModelName=$ModelNo=$productcolor=$cost=$sql1=$sql2=$IMEI=$accessories=$row="";
    
    ?>
    
    <?php
    //Check the request method and take the value in all variables
    if ($_SERVER["REQUEST_METHOD"]=="POST"){
    
    $company=$_POST['Company'];
    $ModelName = $_POST['ModelName'];
    $ModelNo = $_POST['ModelNo'];
    $productcolor = $_POST['productcolor'];
    $cost =$_POST['cost'];
    $IMEI=$_POST['imei'];
    $accessories=$_POST['accessories'];
    }
    
    
    // Connect to database and get the no of rows in $num
    $con = mysqli_connect("MyWebsite.localdomain","root","pwd123","test_youtube");
    $sql1 = "select M_ID,CompanyName,ModelName,ModelNo,IMEI,productcolor,cost,accessories,dt from mobile_data LIMIT 20";
    
    //check connection
    if(!$con){
        echo "Failed to connect to MySQL: " . mysql_error();
    }
    
    $retval1 = mysqli_query($con,$sql1);
    
    echo "<table style='width:100%;'><tr><th>M_ID</th><th>Company</th><th>Model Name</th><th>Model No</th><th>IMEI</th><th>Color</th><th>Cost</th><th>Accessories</th><th>Date</th></tr>";
    //Show all records on the home page
    //echo count(mysqli_fetch_array($retval1));
        while($row= mysqli_fetch_array($retval1)){
            echo "<tr><td>".$row["M_ID"]."<td><td>".$row["CompanyName"]."<td><td>".$row["ModelName"]."<td><td>".$row["ModelNo"]."<td><td>".$row["IMEI"]."<td><td>".$row["productcolor"]."<td><td>".$row["cost"]."<td><td>".$row["accessories"]."<td><td>".$row["dt"]."<td><tr>";
        }
    
    echo "</table>";
    // Perform the insert operation on the table when hit the submit button
    if(isset($_POST['submit'])){
    //$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
    //perform insert queries to enter the data when submit button is clicked
        $sql2 = "insert into mobile_data".
                "(M_ID,CompanyName,ModelName,ModelNo,IMEI,productcolor,cost,accessories)".
                "VALUES".
                "('$num','$company','$ModelName','$ModelNo','$IMEI','$productcolor','$cost','$accessories')";
    
        $retval2 = mysqli_query($con,$sql2);
        //if(!$retval2){
        //  die('Could not enter data: ' . mysql_error());  
        //}
    
    }
    
    
    mysqli_close($con);
    ?>
    ;
    
    </body>
    
    </html>
    

1 个答案:

答案 0 :(得分:1)

我在行中发现了错误,

echo "<tr><td>".$row["M_ID"]."<td><td>".$row["CompanyName"]."<td><td>".$row["ModelName"]."<td><td>".$row["ModelNo"]."<td><td>".$row["IMEI"]."<td><td>".$row["productcolor"]."<td><td>".$row["cost"]."<td><td>".$row["accessories"]."<td><td>".$row["dt"]."<td><tr>";

如下所示,

echo "<tr><td>".$row["M_ID"]."</td><td>".$row["CompanyName"]."</td><td>".$row["ModelName"]."</td><td>".$row["ModelNo"]."</td><td>".$row["IMEI"]."</td><td>".$row["productcolor"]."</td><td>".$row["cost"]."</td><td>".$row["accessories"]."</td><td>".$row["dt"]."</td></tr>";