Friday 24 June 2016

How to Fetch Data from Database of all users in table structre, Open Each user profile & Update it? How to Get Value By passing Variable as id?

Create three files & 1 directory create called "upload".
1. alluserfetchdata.php
2. Adminupdateotherprofile.php
3. Adminupdateotherprofileprocess.php

1> Copy Following code in "alluserfetchdata.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>Fetch data</title>
</head>

<body>
<table width="200" border="1">
 <tr>
<th>No</th>
<th>name</th>
<th>gender</th>
<th>Discription</th>
<th>View</th>
 </tr>

<?php

 $con=mysql_connect('localhost','root','');
if(!$con)
{
echo "Connection error".mysql_error();
}
$db=mysql_select_db('test1');
$sql="select * from registration";
echo $sql;
$i=1;
$result=mysql_query($sql,$con);
while($row=mysql_fetch_array($result))
{
$id=$row['id'];

echo "<tr>";

echo "<td>".$i."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['discreption']."</td>";
echo "<td><a class='link' href=\"Adminupdateotherprofile.php?id=$id\">VIEW & EDIT</a></td>";
echo "</tr>";
$i++;
}
?>
</table>

</body>
</html>


2> Copy Following code in "Adminupdateotherprofile.php" file.
      Here,We Get data by passing id as variable & get all that Id Data.
<!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>Selected person data</title>
<style>
input[type=text]{color:#0F3; width:300px; outline:red;}
</style>
</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=".$_GET['id'];
//echo $sql;
$result=mysql_query($sql,$con);
while($row=mysql_fetch_array($result))
{
?>
<form method="post" action="Adminupdateotherprofileprocess.php" enctype="multipart/form-data">
<table width="200" border="1">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
 <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>

 

3> Copy Following code in "Adminupdateotherprofileprocess.php" file.
     UPDATE profile here.
<?php
session_start();
$id=$_POST['id'];
$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