Update code not running in php.. session is not getting initialised

this is my admin_home.php

<?php
session_start();

include_once 'dbconnect.php';
?>

<!DOCTYPE html>
<html>
<title>Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/w3.css">
<body>
<div class="w3-container">
<h2> Baidyanath Ayurvedic Items </h2>
<div class="w3-bar w3-border w3-light-grey w3-center">
  <a style="width:25%" href="add_admin2.php" class="w3-bar-item w3-button  w3-hover-blue w3-mobile">ADD</a>
 <a style="width:25%" href="update_admin.php" class="w3-bar-item w3-button w3-hover-blue  w3-mobile"> UPDATE</a>
  <a style="width:25%" href="#" class="w3-bar-item w3-button w3-hover-blue  w3-mobile">DELETE</a>
  <a style="width:25%" href="logout.php" class="w3-bar-item w3-button w3-hover-blue w3-mobile">Logout</a>
</div>
</div>
</body>
</html>

this is my update_admin.php

<?php

	error_reporting( ~E_NOTICE );
	
	require_once 'dbconnect.php';
	
	if(isset($_GET['hid']) && !empty($_GET['hid']))
	{
		$id = $_GET['hid'];
		$stmt_edit = $con->prepare('SELECT Hname, image, Hdesc, Hben, Pcure, Scure, cost FROM herbs WHERE hid =:id');
		$stmt_edit->execute(array(':hid'=>$id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: admin_home.php");
	}
	
	
	
	if(isset($_POST['u_uplaod']))
	{
     $Hname= $_POST['Hname'];
	$Hdesc= $_POST['Hdesc'];
	$Hben = $_POST['Hben'];
	$Pcure = $_POST['Pcure'];
	$Scure = $_POST['Scure'];
	$cost= $_POST['cost'];




		$imgFile = $_FILES['file']['name'];
		$tmp_dir = $_FILES['file']['tmp_name'];
		$imgSize = $_FILES['file']['size'];
					
		if($imgFile)
		{
			$upload_dir = '/Applications/XAMPP/xamppfiles/htdocs/p5/upload/'; // upload directory	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
			$image = rand(1000,1000000).".".$imgExt;
			if(in_array($imgExt, $valid_extensions))
			{			
				if($imgSize < 5000000)
				{
					unlink($upload_dir.$edit_row['file']);
					move_uploaded_file($tmp_dir,$upload_dir.$image);
				}
				else
				{
					$errMSG = "Sorry, your file is too large it should be less then 5MB";
				}
			}
			else
			{
				$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";		
			}	
		}
		else
		{
			// if no image selected the old image remain as it is.
			$image = $edit_row['file']; // old image from database
		}	
						
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE herbs 
									     SET Hname=:Hname, 
										     image=:image, 
										     Hdesc=:Hdesc,
										     Hben=:Hben,
										     Pcure=:Pcure,
										     Scure=:Scure,
										     cost=:cost 
								       WHERE uid=:id');
			$stmt->bindParam(':Hname',$Hname);
			$stmt->bindParam(':image',$image);
			$stmt->bindParam(':Hdesc',$Hdesc);
			$stmt->bindParam(':Hben',$Hben);
			$stmt->bindParam(':Pcure',$Pcure);
			$stmt->bindParam(':Scure',$Scure);
			$stmt->bindParam(':cost',$cost);
			$stmt->bindParam(':hid',$id);
				
			if($stmt->execute())
			{


				?>

                <script>
				alert('Successfully Updated ...');
				window.location.href='admin_home.php';
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>

<!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> Update</title>

<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">

<!-- custom stylesheet -->
<link rel="stylesheet" href="style.css">

<!-- Latest compiled and minified JavaScript -->
<script src="bootstrap/js/bootstrap.min.js"></script>

<script src="jquery-1.11.3-jquery.min.js"></script>
</head>
<body>

<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">
 
        <div class="navbar-header">
            <a class="navbar-brand" href="#" title='Baidyanath Ayurvedic'>Baidyanath Ayurvedic </a>
        </div>
 
    </div>
</div>


<div class="container">


	<div class="page-header">
    	<h1 class="h2">update Herbs </h1>
    </div>

<div class="clearfix"></div>

<form method="post" enctype="multipart/form-data" class="form-horizontal">
	
    
    <?php
	if(isset($errMSG)){
		?>
        <div class="alert alert-danger">
          <span class="glyphicon glyphicon-info-sign"></span> &nbsp; <?php echo $errMSG; ?>
        </div>
        <?php
	}
	?>
   
    
	<table class="table table-bordered table-responsive">
	
    <tr>
    	<td><label class="control-label"> Herb Name.</label></td>
        <td><input class="form-control" type="text" name="Hname" value="<?php echo $Hname; ?>" required /></td>
    </tr>
    
    
    <tr>
    	<td><label class="control-label">Image</label></td>
        <td>
        	<p><img src="user_images/<?php echo $image; ?>" height="150" width="150" /></p>
        	<input class="input-group" type="file" name="file" accept="image/*" />
        </td>
    </tr>

    <tr>
    	<td><label class="control-label">Herb Desc</label></td>
        <td><input class="form-control" type="textarea" name="Hdesc" value="<?php echo $Hdesc; ?>" required /></td>
    </tr>
    
      <tr>
    	<td><label class="control-label">Herb Benefits</label></td>
        <td><input class="form-control" type="textarea" name="Hben" value="<?php echo $Hben; ?>" required /></td>
    </tr>


  <tr>
    	<td><label class="control-label">Primary Cure</label></td>
        <td><input class="form-control" type="textarea" name="Pcure" value="<?php echo $Pcure; ?>" required /></td>
    </tr>

      <tr>
    	<td><label class="control-label">Secondary Cure</label></td>
        <td><input class="form-control" type="text" name="Scure" value="<?php echo $Scure; ?>" required /></td>
    </tr>

      <tr>
    	<td><label class="control-label">cost</label></td>
        <td><input class="form-control" type="text" name="cost" value="<?php echo $cost; ?>" required /></td>
    </tr>
    <tr>
        <td colspan="2"><button type="submit" name="u_update" class="btn btn-default">
        <span class="glyphicon glyphicon-save"></span> Update
        </button>
        
        <a class="btn btn-default" href="admin_home.php"> <span class="glyphicon glyphicon-backward"></span> cancel </a>
        
        </td>
    </tr>
    
    </table>
    
</form>


<div class="alert alert-info">
    <strong> ! </strong> <a href="#">Baidyanath Ayurvedic </a> 
</div>

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

this is my sql database code

CREATE DATABASE `testdb`;
USE `testdb`;
CREATE TABLE IF NOT EXISTS `herbs'` (
  `hid` int(8) NOT NULL AUTO_INCREMENT,
  `Hname` varchar(30) NOT NULL,
  `image` BLOB NOT NULL,
  `Hdesc` varchar(40) NOT NULL,
  `Hben` varchar(40) NOT NULL,
`Pcure` varchar(40) NOT NULL,
`Scure` varchar(40) NOT NULL,
`cost` int(100) NOT NULL, 
PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

while i run my update_admin.php code it redirects to admin_home… i dont know why but i feel there something wrong with my session code… i have tried a lot of different way and searched but couldn’t find a solution… can anyone plzz help me in this… :frowning:

I use Xampp 5.6.30-0 in Mac OS … There are no errors in the code as far as i have done.

Hi,

yes you will always be redirected if in your URL there is no “hid” parameter, with a value.
So try the update admin URL with “?hid=something”

Also:

$stmt_edit = $con->prepare('SELECT Hname, image, Hdesc, Hben, Pcure, Scure, cost FROM herbs WHERE hid =:id');
$stmt_edit->execute(array(':hid'=>$id));

the parameter name in the query string is :id, but you pass in :hid to execute it. Do you not get an error when you execute the query, or does it not execute because of the point @goncalvesromain pointed out?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.