Asd
Asd

Reputation: 103

insert value from sql database to html table

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table id="users" border="1">
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Age</td>
            <td>Email</td>
            <td>DOB</td>
        </tr>
        <tr>
            <td>1</td>
            <td><input size=25 type="text" id="Name" readonly=true/></td>
            <td><input size=10 type="text" id="Age" readonly=true/></td>
            <td><input size=25 type="text" id="Email" readonly=true/></td>
            <td><input size=10 type="date" id="DOB" readonly=true/></td>
        </tr>
    </table>
<?php
  $con = mysql_connect("127.0.0.1","root","test") or die(mysql_error());
  $db = mysql_select_db("lr") or die(mysql_error());
  $sql=mysql_query("SELECT * FROM user ")or die(mysql_error());
  $count=0;

while($row = mysql_fetch_array($sql))
{
    $name=$row['Name'];
    $age=$row['age'];
    $email=$row['Email'];
    $dob=$row['DOB'];

    echo  '<table id="users" border="">
        <tr>
            <td>$count</td>
            <td>$name</td>
            <td>$age</td>
            <td>$email</td>
            <td>$dob</td>
        </tr>
  </table>  ';  
$count++;   

}

?>
<form id="form1" name="form1" method="post" action="add.php">
<input type="submit" name="Add" id="Add" value="Add" />
<input type="submit" name="Remove" id="Remove" value="Remove" />
<input type="submit" name="Edit" id="Edit" value="Edit" />

</form>

</body>
</html>

this is what i have tried also have retrieved data from mysql database but unable to show it on my html table. the above code does everything except showing values on the table. do check the while loop that print value to the table

Upvotes: 0

Views: 8152

Answers (3)

GautamD31
GautamD31

Reputation: 28763

Try like this

echo  '<table id="users" border="">';
while($row = mysql_fetch_array($sql))
{
   $name=$row['Name'];
   $age=$row['age'];
   $email=$row['Email'];
   $dob=$row['DOB'];

   echo 
       '<tr>
           <td>'.$count.'</td>
           <td>'.$name.'</td>
           <td>'.$age.'</td>
           <td>'.$email.'</td>
           <td>'.$dob.'</td>
       </tr>';

   $count++;   
}
echo ' </table>  ';

And try to use mysqli_* functions or PDO statements due to the mysql_* functions are deprecated.

Upvotes: 2

Shankar Akunuri
Shankar Akunuri

Reputation: 187

Once check $sql query getting the rows by putting print_r($row); in while loop after that do some modifications in while loop

while($row = mysql_fetch_array($sql)){
echo  "<table id='users' border=''>
    <tr>
        <td>".$count."</td>
        <td>".$row['Name']."</td>
        <td>".$row['age']."</td>
        <td>".$row['Email']."</td>
        <td>".$row['DOB']."</td>
   </tr> </table>";  $count++;   }

Upvotes: 0

user166560
user166560

Reputation:

Variables inside of single-quoted strings aren't expanded.

From Strings:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

You have to use double-quotes around your string, then single-quotes around your HTML properties inside of the string.

while($row = mysql_fetch_array($sql))
{
    $name=$row['Name'];
    $age=$row['age'];
    $email=$row['Email'];
    $dob=$row['DOB'];

    echo  "<table id='users' border=''>
        <tr>
            <td>$count</td>
            <td>$name</td>
            <td>$age</td>
            <td>$email</td>
            <td>$dob</td>
        </tr>
  </table>";

Upvotes: 0

Related Questions