| 
 | 1 | +<?php session_start(); ?>  | 
 | 2 | + | 
 | 3 | +<?php  | 
 | 4 | +if(!isset($_SESSION['valid'])) {  | 
 | 5 | +	header('Location: login.php');  | 
 | 6 | +}  | 
 | 7 | +?>  | 
 | 8 | + | 
 | 9 | +<?php  | 
 | 10 | +// including the database connection file  | 
 | 11 | +include_once("connection.php");  | 
 | 12 | + | 
 | 13 | +if(isset($_POST['update']))  | 
 | 14 | +{	  | 
 | 15 | +	$id = $_POST['id'];  | 
 | 16 | +	  | 
 | 17 | +	$name = $_POST['name'];  | 
 | 18 | +	$qty = $_POST['qty'];  | 
 | 19 | +	$price = $_POST['price'];	  | 
 | 20 | +	  | 
 | 21 | +	// checking empty fields  | 
 | 22 | +	if(empty($name) || empty($qty) || empty($price)) {  | 
 | 23 | +				  | 
 | 24 | +		if(empty($name)) {  | 
 | 25 | +			echo "<font color='red'>Name field is empty.</font><br/>";  | 
 | 26 | +		}  | 
 | 27 | +		  | 
 | 28 | +		if(empty($qty)) {  | 
 | 29 | +			echo "<font color='red'>Quantity field is empty.</font><br/>";  | 
 | 30 | +		}  | 
 | 31 | +		  | 
 | 32 | +		if(empty($price)) {  | 
 | 33 | +			echo "<font color='red'>Price field is empty.</font><br/>";  | 
 | 34 | +		}		  | 
 | 35 | +	} else {	  | 
 | 36 | +		//updating the table  | 
 | 37 | +		$result = mysql_query("UPDATE products SET name='$name', qty='$qty', price='$price' WHERE id=$id");  | 
 | 38 | +		  | 
 | 39 | +		//redirectig to the display page. In our case, it is view.php  | 
 | 40 | +		header("Location: view.php");  | 
 | 41 | +	}  | 
 | 42 | +}  | 
 | 43 | +?>  | 
 | 44 | +<?php  | 
 | 45 | +//getting id from url  | 
 | 46 | +$id = $_GET['id'];  | 
 | 47 | + | 
 | 48 | +//selecting data associated with this particular id  | 
 | 49 | +$result = mysql_query("SELECT * FROM products WHERE id=$id");  | 
 | 50 | + | 
 | 51 | +while($res = mysql_fetch_array($result))  | 
 | 52 | +{  | 
 | 53 | +	$name = $res['name'];  | 
 | 54 | +	$qty = $res['qty'];  | 
 | 55 | +	$price = $res['price'];  | 
 | 56 | +}  | 
 | 57 | +?>  | 
 | 58 | +<html>  | 
 | 59 | +<head>	  | 
 | 60 | +	<title>Edit Data</title>  | 
 | 61 | +</head>  | 
 | 62 | + | 
 | 63 | +<body>  | 
 | 64 | +	<a href="index.php">Home</a> | <a href="view.php">View Products</a> | <a href="logout.php">Logout</a>  | 
 | 65 | +	<br/><br/>  | 
 | 66 | +	  | 
 | 67 | +	<form name="form1" method="post" action="edit.php">  | 
 | 68 | +		<table border="0">  | 
 | 69 | +			<tr>   | 
 | 70 | +				<td>Name</td>  | 
 | 71 | +				<td><input type="text" name="name" value=<?php echo $name;?>></td>  | 
 | 72 | +			</tr>  | 
 | 73 | +			<tr>   | 
 | 74 | +				<td>Quantity</td>  | 
 | 75 | +				<td><input type="text" name="qty" value=<?php echo $qty;?>></td>  | 
 | 76 | +			</tr>  | 
 | 77 | +			<tr>   | 
 | 78 | +				<td>Price</td>  | 
 | 79 | +				<td><input type="text" name="price" value=<?php echo $price;?>></td>  | 
 | 80 | +			</tr>  | 
 | 81 | +			<tr>  | 
 | 82 | +				<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>  | 
 | 83 | +				<td><input type="submit" name="update" value="Update"></td>  | 
 | 84 | +			</tr>  | 
 | 85 | +		</table>  | 
 | 86 | +	</form>  | 
 | 87 | +</body>  | 
 | 88 | +</html>  | 
0 commit comments