如何使用PHP更新从数据库显示的数据?

时间:2016-11-08 06:44:16

标签: php html mysql

我从html插入数据并将其存储在mysql数据库中,并将数据从数据库中检索到html,现在我已经完成了如何删除html中显示的数据,我的任务是如何更新显示。我删除的代码是: 以下是我的HTML代码:

<html> 
 <head>
<title>STUDENT_DATA</title>
 </head>
  <body>
  <form action="tab.php" method="post" >
<center>
sname: <input type="text" name="sname" required><br></br>
sno:<input type="text" name="sno"><br></br>
marks:<input type="text" name="marks"><br></br>
class:<input type="text" name="class"><br></br>
phno:<input type="text" name="phno" onkeypress='return event.charCode >
= 48 && event.charCode <= 57'><br></br>
DOB:<input type="date" placeholder="DD-MM-YYYY" 
required pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}"       
name="DOB"/><br></br>
 <button>submit</button></br>
    </center>
  </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( "student",$connection);
   if (!$select_db)
      {
  die("Database Selection Failed" . mysql_error());
       }
   $sql = "SELECT * FROM hello1 ";     
   $result = mysql_query($sql) or die(mysql_error());
   ?>
  <table border="2" style= " margin: 0 auto;" id="myTable">
   <thead>
    <tr>
      <th>sname</th>
      <th>sno</th>
      <th>marks</th>
      <th>class</th>
      <th>phno</th>
      <th>DOB</th>
    </tr>
   </thead>
   <tbody>
   <?php
   while($row = mysql_fetch_array($result))
     {
    echo "<tr>";
    echo "<td>" . $row['sname'] . "</td>";
    echo "<td>" . $row['sno'] . "</td>";
    echo "<td>" . $row['marks'] . "</td>";
    echo "<td>" . $row['class'] . "</td>";
    echo "<td>" . $row['phno'] . "</td>";
    echo "<td>" . $row['DOB'] . "</td>";
    echo "<td><a href='delete.php?did=".$row['sname']."'>Delete</a></td>";
    echo "</tr>";
      }
    ?>
        </tbody>
      </table>
     </body>
     </html>

以下是我的删除PHP代码:

  <?php
  $connection = mysql_connect('localhost', 'root','');
  if (!$connection)
    {
      die("Database Connection Failed" . mysql_error());
     }
  $select_db = mysql_select_db( "student",$connection);
  if (!$select_db)
     {
      die("Database Selection Failed" . mysql_error());
     }
    ?>
  <?php
  if(isset($_GET['did'])) {
  $delete_id = $_GET['did'];
  $sql = mysql_query("DELETE FROM hello1 WHERE sname = '".$delete_id."'");
  if($sql) {
    echo "<br/><br/><span>deleted successfully...!!</span>";
 }
  else 
  {
    echo "ERROR";
  }
}

&GT;

以下是我在update.php中尝试过的内容

 <?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()); } ?> 
<?php if(isset($_GET['did'])) 
{ 

 $update_id = $_GET['did']; $sql = mysql_query("UPDATE FROM venu WHERE id = '".$update_id."'"); 
if($sql) { 
    echo "<br/><br/><span>updated successfully...!!</span>";    
} else { 
    echo "ERROR"; 
} 

} ?>

0 个答案:

没有答案