无法更改数据库

时间:2014-05-17 16:23:56

标签: php database

我的目标是使用更新查询更新数据库中的值。在我的第一页上,我刚刚在网页中显示了数据库表。然后通过使用超链接我必须点击编辑第二页“edit.php”。在第一页上我必须得到 id的值并将其发送到第二页。显示输入表单,其中随意获取值但 Id 通过隐藏标记。在第三页上实现了值查询,但缺少 id 的值。

第一页

<html>
<head>
 <title>Assignment</title>
 </head>

<body>
 <?php
     $con=mysql_connect("localhost","root","");
     // Check connection
    if (!mysql_connect()) {
        echo "Failed to connect to MySQL: " . mysql_connect_error();
    }
     $db=mysql_select_db("assignment",$con);

     $result = mysql_query("SELECT * FROM teacher ",$con);
    ?><table cellpadding="2px" border="2px"><?php
    while($row = mysql_fetch_array($result)) {
     ?> <tr>
          <td><a href="edit.php?id=<?php       
        echo $row['id'];  ?>">Edit</a > <a href="delete.php">Delete</a>
                 </td><td>
             <?php       
        echo $row['id'];  ?></td><td> <?php echo $row['name'];?></td><td><?php echo $row['program']; ?></td>
               <?php   }
          ?></table><?php
    mysql_close($con);
 ?>

</body>
</html>  

Secnod Page edit.php

<html>
<head>
      <title>Assignment Edit</title>
    </head>
<body>
 <?php
    $id = $_GET['id'];
 ?>
    <form action="update.php" method="get">
       Address <input type="text" name="program"><br>
       <input type="hidden" name="id" value='<?php $id?>'>
        <input type="submit" name="submit">
    </form>

</body> 
</html>

第三页update.php

<html>
<head>
      <title>Update Page</title>
      </head>

      <body>

  <?php
       $add=$_GET['program'];
       $id=$_GET['id'];
       $con=mysql_connect("localhost","root","");
     // Check connection
       if (!mysql_connect()) {
          echo "Failed to connect to MySQL: " . mysql_connect_error();
        }
       $db=mysql_select_db("assignment",$con);
        $query = "UPDATE teacher SET program='$add' WHERE id =".$id;
        echo $query;
       $result = mysql_query($query,$con);

      /* while($row = mysql_fetch_array($result)) {
          echo $row['id'] ." " .  $row['name']." ".  $row['address']."<br>"; 
               }
       mysql_close($con);
  */
  ?>
  </body>
</html>

输出

UPDATE老师SET program ='openSource'WHERE id =

1 个答案:

答案 0 :(得分:0)

你需要改变这个

   <input type="hidden" name="id" value='<?php $id?>'>

   <input type="hidden" name="id" value='<?php echo $id?>'>

(或)

   <input type="hidden" name="id" value='<?=$id?>'>