Shesharm
Shesharm

Reputation: 35

html Table taking extra blank values while fetching the row from mysql using php

  1. I have one welcome.php file where i need to fetch the records from mysql database to html table on the page.
  2. It is fetching the record but placing one blank field after each column fields. e.g. if there are 6 columns. Then it is displaying 12 fields. 6 actual fields and one blank field after each field. So, html table structure enter code herealso disturbs.

I have used mysqli_fetch_array and put a while loop to fetch 20 records.I am not sure i am making mistake while fetching record or there is a problem in html table itself. Please find the code below and let me know what wrong i am doing. Thanks.

 <?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>

Upvotes: 0

Views: 581

Answers (1)

Akilan
Akilan

Reputation: 1717

I found the error in the line,

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>";

correct like below,

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>";

Upvotes: 1

Related Questions