【问题标题】:html Table taking extra blank values while fetching the row from mysql using phphtml表在使用php从mysql获取行时采用额外的空白值
【发布时间】:2014-11-21 12:47:18
【问题描述】:
  1. 我有一个welcome.php 文件,我需要从mysql 数据库中获取记录到页面上的html 表中。
  2. 它正在获取记录,但在每列字段之后放置一个空白字段。例如如果有 6 列。然后它显示 12 个字段。 6 个实际字段和每个字段后的一个空白字段。所以,html 表结构enter code here也打扰了。

我使用了 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>

【问题讨论】:

  • 你没有关闭你的行/单元格,你只是开始一个新的行/单元格 -> echo "&lt;tr&gt;&lt;td&gt;".$row["M_ID"]."&lt;td&gt;&lt;td&gt;...[your code]...&lt;td&gt;&lt;td&gt;".$row["dt"]."&lt;td&gt;&lt;tr&gt;"; 例如最后一个&lt;tr&gt;应该是&lt;/tr&gt;。这是一个 html 问题,而不是一个 php 问题
  • @Shekhar,如果您从我的回答中得到了解决方案,请接受我的回答

标签: php mysql


【解决方案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>";

【讨论】:

  • 是的,你是对的 Akilan.. 我仍然没有检查它是否工作正常.. 但这似乎是实际问题..感谢您的帮助 :)
猜你喜欢
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
相关资源
最近更新 更多