Friday 24 June 2016

How to Fetch Data from Database as Single person Profile & update Profile data?

Create two files &  1 directory create called "upload".
1. profile.php
2. profileupdateprocess.php


1> Copy Following code in "profile.php" file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Profile (Fetch data)</title>
</head>

<body>
<?php
session_start();
$id=$_SESSION['id'];
//echo $_SESSION['id'];
$con=mysql_connect('localhost','root','');
if(!$con)
{
echo "Connection error".mysql_error();
}
$db=mysql_select_db('test1');
$sql="select * from registration where id='$id'";
//echo $sql;
$result=mysql_query($sql,$con);
while($row=mysql_fetch_array($result))
{
    ?>
    <form method="post" action="profileupdateprocess.php" enctype="multipart/form-data">
<table width="200" border="1">
  <tr>
    <td>Name:</td>
    <td><input type="text" name="name" value="<?php echo $row['name']; ?>"/></td>
  </tr>
  <tr>
    <td>Gender:</td>
    <td><input type="radio" name="gender" value="<?php echo $row['gender']; ?>" checked="checked"/><?php echo $row['gender']; ?>
<?php
    if($row['gender']=='Male')
    {?>
<input type="radio" name="gender" value="Female"/>Female
    <?php
}
    else
    {?>
<input type="radio" name="gender" value="Male"/>Male
    <?php
}
?></td>
  </tr>
  <tr>
    <td>discription</td>
    <td><textarea name="discreption" ><?php echo $row['discreption']; ?></textarea></td>
  </tr>
  <tr>
    <td>Photo:</td>
    <td><input type="file" name="photo" value="<?php echo $row['photo']; ?>" required="required"/></td>
  </tr>
  <tr>
    <td>Image:</td>
    <td><img src="<?php echo $row['photo']; ?>" width="300px" height="300px"/></td>
  </tr>
</table>
<input type="submit" name="update" value="UPDATE" />
</form>
<?php
}
?>
</body>
</html>


2> Copy Following code in "profileupdateprocess.php" file.
<?php
session_start();
$name=$_POST['name'];
$gender=$_POST['gender'];
//$photo=$_POST['photo'];
$discreption=$_POST['discreption'];


$dir="upload/";
$path=$dir.basename($_FILES['photo']['name']);
move_uploaded_file($_FILES['photo']['tmp_name'],$path);

$id=$_SESSION['id'];

$con=mysql_connect('localhost','root','');
if(!$con)
{
echo "Connection error".mysql_error();
}
$db=mysql_select_db('test1');
$sql="UPDATE `registration` SET `name`='$name',`gender`='$gender',`photo`='$path',`discreption`='$discreption' WHERE id='$id'";
echo $sql;
$result=mysql_query($sql,$con);
if($result>0)
{
echo "Successfull";
}
else
{
echo "fail";
}



To download code Click Here.

For more Interesting, Useful Article & codes visit IT New Code.

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

0 comments:

Post a Comment

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah