Wednesday 14 March 2012

Example Of Connectivity With MySql


·         connection.php

<?php
          $con = mysql_connect('localhost','root','');
          mysql_select_db('college',$con);
?>

·         view.php

<?php  require("connection.php");
          $query = "select * from student_master";
         
          $rs = mysql_query($query);
?>
          <table align="center" width="50%" border="1">
                   <tr>
                             <th align="center">ID</th>
                             <th align="center">Name</th>
                             <th align="center">City</th>
                             <th align="center">Phone</th>
                             <th align="center">Action</th>
                   </tr>
<?php
          while($row = mysql_fetch_array($rs))
          {
                   echo "<tr><td>".$row['id']."</td>";
                   echo "<td>".$row['name']."</td>";
                   echo "<td>".$row['city']."</td>";
                   echo "<td>".$row['phone']."</td>";
echo "<td align='center'><a href='edit.php?e=".$row['id']."'>Edit</a> &nbsp;|&nbsp;<a href='delete.php?d=".$row['id']."'>Delete</a> </td></tr>";
          }
?>
                   <tr>
                             <td colspan="5" align="center"><a href="view.php">View Records</a></td>
                   </tr>
                   <tr>
                             <td colspan="5" align="center"><a href="insert.html">Add New Records</a></td>
                   </tr>
          </table>

·         insert.html

<html>
<head>
                    <title>Insert Form</title>
</head>
<body>
<form name="insert" action="insert.php" method="post">
                             <table align="center" width="50%" cellpadding="5" cellspacing="5">
                   <tr>
                             <td>Name: - </td>
<td><input type="text" name="sname" id="sname" /></td>
                   </tr>
                   <tr>
                             <td>City: - </td>
<td><input type="text" name="scity" id="scity" /></td>
                   </tr>
                   <tr>
                             <td>Phone: - </td>
<td><input type="text" name="sph" id="sph" /></td>
                   </tr>
                   <tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="SAVE" /> </td>
                   </tr>
          </table>
</form>
</body>
<html>

·         insert.php

<?php require("connection.php");
          $name = $_POST['sname'];
          $city = $_POST['scity'];
          $phone = $_POST['sph'];
         
$query = "insert into student_master (name,city,phone) values('".$name."','".$city."','".$phone."')";
         
          mysql_query($query);
          echo "Record Successfully Inserted";
?>
·         delete.php

<?php
          $id = $_GET['d'];
                  
          $con = mysql_connect('localhost','root','');
          mysql_select_db('college',$con);
         
         
          $query = "delete from student_master where id=".$id;
         
          mysql_query($query);
          echo "Record Successfully Deleted<br />";
?>
          <a href="view.php">View Records</a><br />
          <a href="insert.html">Add New Records</a>

·         edit.php

<?php require("connection.php");
          $id = $_GET['j'];
         
          $query = "select * from student_master where id=".$id;
         
          $rs = mysql_query($query);
          $count = mysql_num_rows($rs);
         
          if($count>0)
          {
                   $row = mysql_fetch_array($rs);
                   ?>
                   <form name="update" action="update.php" method="post">
<table align="center" width="50%" cellpadding="5" cellspacing="5">
                   <tr>
                             <td>Name: - </td>
                             <td>
<input type="text" name="sname" id="sname" value="<?php echo $row['name']; ?>" />
</td>
                   </tr>
                   <tr>
                             <td>City: - </td>
                             <td>
<input type="text" name="scity" id="scity" value="<?php echo $row['city']; ?>"  />
</td>
                   </tr>
                   <tr>
                             <td>Phone: - </td>
<td>
<input type="text" name="sph" id="sph" value="<?php echo $row['phone']; ?>"  />
</td>
                   </tr>
                   <tr>
<td colspan="2" align="center">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" /><input type="submit" name="submit" value="UPDATE" />
</td>
                   </tr>
          </table>
                    </form>
                   <?php
          }
?>

·         update.php

<?php require("connection.php");
          $id = $_POST['id'];
          $name = $_POST['sname'];
          $city = $_POST['scity'];
          $phone = $_POST['sph'];
         
          $query = "update student_master set name='".$name."', city = '".$city."', phone='".$phone."' where id=".$id;
         
          mysql_query($query);
          echo "Record Successfully Updated";
?>

Posted By : Hemangi Zala

1 comment: