How to edit and update data in php

How to edit and update data in php:

home

edit

Index.php:

——————————————————————

<?php
session_start();
$con=mysql_connect(“localhost”,”root”,””) or die(“mysql is not connected”);
$database=mysql_select_db(“myproject”,$con) or die(“database is not connected”);
if($_SESSION[‘username’]==” && $_SESSION[‘userpassword’]==”)
{
$_SESSION[‘msg’]=”Please enter login details”;
header(“Location:index.php”);
}
?>
<html>
<head>
<title>user home page</title>

</head>

<body bgcolor=”#FFFFCC”>

<div style=”background-color:#006699; height:150px; width:100%; float:left; border:1px #000000 solid;”><div style=”float:right; margin-top:20px; margin-right:10px; color:#FFFFFF;”> Welcome USER <a href=”logout.php”>Logout</a></div></div>
<div style=”float:left; width:100%”>
<div style=”float:left; background-color:#00CCFF; width:24%; border:1px #000000 solid;”></div>
<div style=”background-color:#CCCCCC; height:50px; float:right; width:75%; border:1px #000000 solid;”>
<div style=”float:left; height:50px; width:100%;”>
<div style=”float:left; margin-top:20px; margin-left:10px;”>Manage User</div>
<div style=”float:right; margin-top:20px; margin-right:10px;”><a href=”addform.php” style=”text-decoration:none;”><input type=”button” name=”button” value=”add new” /></a></div>
</div>
</div>
<form name=”frm” action=”” method=”post”>
<div style=”float:right; background-color:#006666; width:75%; border:1px #000000 solid;”>
<div style=”float:left; height:50px; width:100%; padding-top:10px;”>
<div style=”float:left; margin-left:100px;”>Artist Name:<input type=”text” name=”artistname” value=”” /></div>
<div style=”float:left; margin-left:100px;”><input type=”submit” name=”search” value=”search” /></div>
<div style=”float:right; margin-right:10px;”>View All</div>

</div>
<?php
$query=”select * from artists”;
$result=mysql_query($query);
?>
<table width=”100%” border=”1″ bgcolor=”#FFCCCC”>
<tr  bgcolor=”#0099CC”><th>S.No</th>
<td>Artist Name</td>
<td>Adress</td>
<td>email</td>
<td>Phone</td>
<td>Image</td>
<td>Description</td>
<td>Status</td>
<td>Feture</td>
<td>Action</td>
<td></td>

</tr>
<?php
$i=1;
while($data=mysql_fetch_array($result))
{
//$id=$data[‘id’];
//$status=$data[‘status’];
?>
<tr  bgcolor=”#0099CC”>
<td><?php echo $i++; ?></td>
<td><?php echo $data[‘artistname’]; ?></td>
<td><?php echo $data[‘address’]; ?></td>
<td><?php echo $data[’email’]; ?></td>
<td><?php echo $data[‘phone’]; ?></td>
<td><img src=”<?php echo “img/”.$data[‘image_name’]; ?>” width=”88″ height=”96″/></td>
<td><?php echo $data[‘description’]; ?></td>
<td><?php echo $data[‘status’] ?></td>
<td></td>
<td><a href=”edit.php?id=<?php echo $data[‘id’] ?>”>Edit</a></td>

<td><input type=”checkbox” name=”checkbox[]” value=”<?php echo $data[‘id’]; ?>” /></td>
</tr>

<?php
}
?>

</table>

</div>
</form>
</div>
</body>
</html>

——————————————————————

Edit.php:

<?php
session_start();
$con=mysql_connect(“localhost”,”root”,””) or die(“mysql is not connected”);
$database=mysql_select_db(“myproject”,$con) or die(“database is not connected”);
$id=$_REQUEST[‘id’];

$query=”SELECT * FROM artists where id=’$id'”;
$result=mysql_query($query);
$data=mysql_fetch_array($result);
if(!$data)
{
echo “id not found”;
}
else
{
?>
<html>
<head>
<title>add form</title>
</head>

<body bgcolor=”#FFFFCC”>

<div style=”background-color:#006699; height:150px; width:100%; float:left; border:1px #000000 solid;”></div>
<div style=”float:left; height:600px; width:100%”>
<div style=”float:left; height:600px; background-color:#00CCFF; width:24%; border:1px #000000 solid;”></div>
<div style=”background-color:#CCCCCC; height:600px; float:right; width:75%; border:1px #000000 solid;”>

<form action=”update.php” method=”post” enctype=”multipart/form-data” name=”frm”>
<table align=”center” width=”400″ border=”1″>
<tr><td><input type=”hidden” name=”id” value=”<?php echo $data[‘id’]; ?>” /></td></tr>
<tr><td>Artist Name:</td><td><input type=”text” name=”artistname” value=”<?php echo $data[‘artistname’]; ?>” /></td></tr>
<tr><td>Address:</td><td><input type=”text” name=”address” value=”<?php echo $data[‘address’]; ?>” /></td></tr>
<tr><td>Email:</td><td><input type=”text” name=”email” value=”<?php echo $data[’email’]; ?>” /></td></tr>
<tr><td>Phone:</td><td><input type=”text” name=”phone” value=”<?php echo $data[‘phone’]; ?>” /></td></tr>
<tr><td>Image:</td><td><img src=”<?php echo “img/”.$data[‘image_name’]; ?>” width=”88″ height=”96″/><input type=”file” name=”image_name” /></td></tr>
<tr><td>Description:</td><td><input type=”text” name=”description” value=”<?php echo $data[‘description’]; ?>” /></td></tr>
<tr><td></td><td><input type=”submit” name=”update” value=”update” /></td></tr>
</table>
<?php
}
?>
</form>

</div>
</div>
<div style=”background-color:#006699; height:150px; width:100%; float:left; border:1px #000000 solid;”></div>
</body>
</html>

——————————————————————

Update.php:

<?php
$con=mysql_connect(“localhost”,”root”,””) or die(“mysql is not connected”);
$database=mysql_select_db(“myproject”,$con) or die(“database is not connected”);
if(isset($_POST[‘update’]))
{
$id=$_POST[‘id’];
$artistname=$_POST[‘artistname’];
$address=$_POST[‘address’];
$email=$_POST[’email’];
$phone=$_POST[‘phone’];
$description=$_POST[‘description’];
$image_name=time().$_FILES[“image_name”][“name”];
move_uploaded_file($_FILES[“image_name”][“tmp_name”],”img/”.$image_name);
/**if(($_FILES[“image_name”][“type”] == “image/gif”) || ($_FILES[“image_name”][“type”] == “image/jpeg”))*/

$query=(“update artists set artistname=’$artistname’, address=’$address’, email=’$email’, phone=’$phone’, image_name=’$image_name’, description=’$description’ where id=’$id'”);
mysql_query($query);
header(“Location:home.php”);
}

?>

Please follow and like us:
0

Add a Comment

Your email address will not be published. Required fields are marked *