【发布时间】:2014-11-21 12:47:18
【问题描述】:
- 我有一个welcome.php 文件,我需要从mysql 数据库中获取记录到页面上的html 表中。
- 它正在获取记录,但在每列字段之后放置一个空白字段。例如如果有 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 "<tr><td>".$row["M_ID"]."<td><td>...[your code]...<td><td>".$row["dt"]."<td><tr>";例如最后一个<tr>应该是</tr>。这是一个 html 问题,而不是一个 php 问题 -
@Shekhar,如果您从我的回答中得到了解决方案,请接受我的回答