如何使用PHP在html文本框中显示数据库中的数据

时间:2016-11-04 07:27:20

标签: php html

我在html页面中显示我的表格数据,但我得到空白页面.....

我是php编码的新手请帮助我.....

这是我的HTML代码

<!DOCTYPE html>
<html>  
<head>
<script>  
function validate(){      
var phonenumber=document.myform.phonenumber.value;  
 if(phonenumber.length>=10){  
  alert("Phonenumber must be at least 10 characters");  
  return false;  
  }
  }   
</script>    
</head>
<body  font-color="red">
<form  name ="myform" action="dis.php" method="post"  submit="return validate()">

<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<center><tr>
   <td><h1><marquee> Employee Details</marquee></h1></td>
</tr><center>

<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td align='center'>Name:</td>
    <td><input type='text' name='name' required></td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
    <td align='center'>id:</td>
    <td><input type='text' name='id'></td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
    <td align='center'>roll number:</td>
    <td><input type='text' name='rollnumber'></td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
    <td align='center'>Address:</td>
    <td><input type='text' name='address'></td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
    <td align='center'>Phonenumber:</td>
    <td><input type='text' name='phonenumber'></td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<table border='0' cellpadding='0' cellspacing='0' width='480px' align='center'>
<tr>
    <td align='center'><input type='submit' name='insert' value="insert"></td>
</tr>
<tr>
    <td align='center'><input type='submit' name='update' value="update"></td>
</tr>
</table>
</table>

</table>
</form>
</body>
</html>

这是我的PHP代码

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
    $connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
    die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db)
{
    die("Database Selection Failed" . mysql_error());
}
else
{
    session_start();
    error_reporting(0);
    $name = $_POST['name'];
    $id = $_POST['id'];
    $rollnumber = $_POST['rollnumber'];
    $address = $_POST['address'];
    $phonenumber = $_POST['phonenumber'];

    if(isset($_POST['insert']))
    {
    $sql = mysql_query("SELECT * FROM venu ");     
    $result = mysql_query($sql) or die(mysql_error());
    $n=mysql_num_rows($result);
  if($n > 0)
  {
          echo "<table>";
          echo "<th>name</th>";
          echo "<th>id</th>";
          echo "<th>rollnumber</th>";
          echo "<th>address</th>";
          echo "<th>phonenumber</th>";

            while($row = mysql_fetch_array($result))
            {
              echo "<tr>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['rollnumber'] . "</td>";
                echo "<td>" . $row['address'] . "</td>";
                echo "<td>" . $row['phonenumber'] . "</td>";
                   echo "</tr>";
            }
             echo "</table>";

    } 
    else
  {
        echo "No records matching your query were found.";
    }
  }
  else
{
    echo "ERROR: Could not able to execute.";
} 
}
mysql_close($connection); 
?>
</body>
</html>

我收到错误  只显示空白页面... 请帮帮我....

1 个答案:

答案 0 :(得分:2)

您正在使用mysql_query()两次。改变这个:

$sql = mysql_query("SELECT * FROM venu ");     
$result = mysql_query($sql) or die(mysql_error());

分为:

$sql = "SELECT * FROM venu ";     
$result = mysql_query($sql) or die(mysql_error());