Wednesday, November 21, 2018

How can edit the data in MySql using core php ?

form.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("project");

//edit
if (isset($_REQUEST['edit_id']))
{
$eid=$_REQUEST['edit_id'];
$select="select * from tbl_user where id='$eid' ";
$ex=mysql_query($select);
$rec=mysql_fetch_array($ex);
}
?>
<html>
<head>
<title>insert the data</title>
</head>
<body>
<form method="post">
    <table border="1" align="center">
     <tr>
         <td>name</td>
            <td><input type="text" name="name" value="<?php echo $res['name']; ?>"></td>
        </tr>
        <tr>
         <td>password</td>
            <td><input type="text" name="pass" value="<?php echo $res['password']; ?>"></td>
        </tr>
        <tr>
         <td>gender</td>
< td>
<?php 
    $gen=$rec['gender']; // gender is the database table coulmn name
     if($gen=='male')
      {

?>
            <input type="radio" name="gender" value="male" checked='checked'>male
             <input type="radio" name="gender" value="female">female

<?php 
}  
else  if($gen=='female')
{ ?>
 <input type="radio" name="gender" value="male">male
 <input type="radio" name="gender" value="female" checked='checked'>male
          
<?php 
}
else 
{

?>
<input type="radio" name="gender" value="male">male
   <input type="radio" name="gender" value="female">female

<?php 
}?>
< /td>
        </tr>
        <tr>
         <td>hobby</td>
            <td>
<?php 
    $ex=$rec['hobby'];
    $h=explode("," , $ex);

if (isset($_REQUEST['edit_id']))
{
        if($h[0]=='cricket')
        { ?>
             <input type="checkbox" name="chk[]" value="cricket" checked='checked'>cricket
            <?php
              }
               else
               { ?>
           
            <input type="checkbox" name="chk[]" value="cricket">cricket
<?php 
}  
 if($h[0]=='hockey' || $h[1]=='hockey' )
{ ?>
  <input type="checkbox" name="chk[]" value="hockey" checked='checked'>hockey
            <?php }
               else
               { ?>
               <input type="checkbox" name="chk[]" value="hockey">hockey

<?php }}
else
{
?>
 <input type="checkbox" name="chk[]" value="cricket">cricket
<input type="checkbox" name="chk[]" value="hockey">hockey
<?php }
?>

< /td>
        </tr>
        <tr>
         <td colspan="2"><input type="submit" name="submit" value="submit"></td>
        </tr>
    </table>
    </form>
</body>
</html>


View.php---edit link

<?php
mysql_connect("localhost","root","");
mysql_select_db("project");

$s="select * from user";//select all records from user table
$ex=mysql_query($s);


?>
<html>
<head>
<style type="text/css">
table th{ background:#F93; color:white;}
table td{ background:#FC9;}
</style>
</head>
<body>
<form method="post">
<table  align="center">
<tr>
<th>id</th>
    <th>name</th>
    <th>pass</th>
    <th>gender</th>
    <th>hobby</th>\
< th>action</th>
    
</tr>
<?php
while ($ft=mysql_fetch_array($ex))   / /fetch the records using  mysql_fetch_array with  while loop
{
?>
<tr>
<td><?php echo $ft['id']; ?></td>
    <td><?php echo $ft['name']; ?></td>
    <td><?php echo $ft['pass']; ?></td>
    <td><?php echo $ft['gender']; ?></td>
    <td><?php echo $ft['hobby']; ?></td>
    <td><a href="form.php ? edit_id=<?php echo $ft['id']; ?>">edit</a></td>
</tr>
<?php
}
?>

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

0 comments:

Post a Comment