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>

Related Posts:

  • History Of PHP The History Of Php  PHP is an "HTML-embedded scripting language" primarily used for dynamic Web applications. The first part of this definition means that PHP code can be interspersed with HTML, making it sim… Read More
  • PHP Case Sensitivity PHP Case Sensitivity In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive. In the example below, all three echo statements below are legal (and … Read More
  • Welcome To My Blog                                                               … Read More
  • OOP PHP Login Tutorial   What is a PHP Secure Login System with Registration? Many applications need to register and authenticate users. Some users have developed their own packages for this purpose, others have used existing p… Read More
  • Create a MySQL Database Using MySQLi and PDO The following examples create a database named "myDB": <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection$conn = new mysqli($servername, $us… Read More

0 comments:

Post a Comment