简体   繁体   English

从sql数据库插入值到html表

[英]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. 这是我试过也从mysql数据库中检索数据但无法在我的html表上显示它。 the above code does everything except showing values on the table. 除了在表上显示值之外,上面的代码执行所有操作。 do check the while loop that print value to the table 检查打印值到表的while循环

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. 并尝试使用mysqli_ *函数或PDO语句,因为不推荐使用mysql_ *函数。

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. 注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中出现时不会扩展。

You have to use double-quotes around your string, then single-quotes around your HTML properties inside of the string. 您必须在字符串周围使用双引号,然后在字符串内部的HTML属性周围使用单引号。

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

Once check $sql query getting the rows by putting print_r($row); 检查$sql查询后通过print_r($row);获取行print_r($row); in while loop after that do some modifications in while loop 在while循环之后,在while循环中做一些修改

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++;   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM