Hi all,

I need a big help..I was struggling about 3 /more days with this code..It is not giving me the result I want.It is not that difficult thing.What I want is to display the values in database(name,description,image) according to the id selected. And then it can be edited if want ,after that I want to submit the new values to the database.But it is not running.As I see there is no error in the query ,so I think something wrong with the php code.

Can someone go through it and help me please.Its a great help.Thanks.

<?php
session_start(); //Session Starts.
if(!isset($_SESSION['valid']) || $_SESSION['valid']!="yes"){
header("location:../../admin/adminLogin.php");
//print "invalid login";
exit();
}

require_once '../../connect.php' ;

if (isset($_POST['btnSelectId']))
{

		$cid         = $_POST['catId'];
		
		$sql="SELECT * FROM tbl_category WHERE cat_id ='$cid'";//search using category id 
		
		$result = mysql_query($sql) or die(mysql_error()); //Execute query
		$cat_row = mysql_fetch_array($result);//row containing the row values
		$name = $cat_row['cat_name'];//assign name field value of the database to a variable
		$description = $cat_row['cat_description'];	//assign description field value of the database to a variable  
		$image = $cat_row['cat_image'];//assign image field value of the database to a variable 

}


if (isset($_POST['btnModifyCategory'])){
	
	$cid         = $_POST['catId'];
	$name2        = $_POST['txtName'];
	$description2 = $_POST['catDesc'];
	//$image3		  = $_POST['file'];
	//print_r($_FILES['file']);
	if(isset($_FILES['file']))
	{
			$image2		  = uploadImage();	
	}
	 	
	//update the database with new values.
	if(isset($_FILES['file']))
		{
			if($_FILES['file']['name'] != "")
			{
				$sql2    = "UPDATE tbl_category SET cat_name ='$name2' WHERE cat_id = '$name'";
				$result2 = mysql_query($sql2) or die(mysql_error());
			}
			else
			{
				$sql2    = "UPDATE tbl_category SET cat_name ='$name2' WHERE cat_id = '$name'";
				$result2 = mysql_query($sql2) or die(mysql_error());
			
			}
	}
	print "<script>window.alert('Category Has been deleted')</script>";

	
	//echo "<font face='Verdana' size='2' ><center> Category Has been modified Successfully</font></center>";
	header('Location: modifyCategory.php');   
}
	
	
if (isset($_POST['deleteImg']))
{

	$name3        = $_POST['txtName'];
	
	//Remove the category image from the database
	$sql    = "UPDATE tbl_category SET cat_image='' WHERE cat_id = '$cid'";

	$result2 = mysql_query($sql) or die(mysql_error());
	//unlink($image2);
	//echo "<font face='Verdana' size='2' ><center> Category Image has been deleted Successfully from the database</font></center>";
	header('Location: modifyCategory.php');
}


function uploadImage()
{

	if(isset($_POST["btnModifyCategory"]))
	{
		
				$fname=$_FILES["file"]["name"];
				$fsize=$_FILES["file"]["size"];
				$ftype=$_FILES["file"]["type"];
				$ferror=$_FILES["file"]["error"];
				$ftname=$_FILES["file"]["tmp_name"];
				if ((($ftype == "image/gif")|| ($ftype== "image/jpeg")|| ($ftype == "image/pjpeg"))&& ($fsize < 20000000))
				{
					if ($ferror> 0)
					{
						echo "Return Code: " . $ferror . "<br />";
					}
					else
					{
						echo "Upload: " . $fname . "<br />";
						echo "Type: " . $ftype . "<br />";
						echo "Size: " . ($fsize / 1024) . " Kb<br />";
						echo "Temp file: " . $ftname . "<br />";
						if (file_exists("upload/" . $fname))
						{
							echo $fname . " already exists. ";
						 }
						else
						{
						
							move_uploaded_file($ftname,"upload/". $fname);
							echo "Stored in: " . "upload/" . $fname;
							return $fname;
						}
					}
				}
				else
				{
					echo "Invalid 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=iso-8859-1" />
<title>Lakkam Trade Center</title>


<script language="javascript">
<!--
function Delete(){

var msg;
msg= "Are you sure you want to delete this product image?" ;
var agree=confirm(msg);
if (agree)
return true ;
else
return false ;
}

function Modify(){

var msg;
msg= "Are you sure you want to edit this contents?" ;
var agree=confirm(msg);
if (agree)
return true ;
else
return false ;
}
-->
</script>

<style type="text/css">
<!--

body {
	background-color:#aeb80a ;
}
#Layer1 {
	position:absolute;
	left:459px;
	top:62px;
	width:332px;
	height:37px;
	z-index:1;
}
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif, Vivaldi;
	font-size: 24px;
	color: #FFFFFF;
}
#Layer2 {
	position:absolute;
	left:347px;
	top:158px;
	width:586px;
	height:266px;
	z-index:2;
}
#Layer3 {
	position:absolute;
	left:362px;
	top:89px;
	width:538px;
	height:219px;
	z-index:2;
}
.style3 {
	font-family: Georgia, "Times New Roman", Times, serif;
	font-weight: bold;
	font-style: italic;
	font-size: 14px;
	color: #FFFFFF;
}
-->
</style>
</head>

<body>
<div class="style1" id="Layer1" align="center"> ::Modify Category::</div>

<div id="Layer3" align="center">
<form action="" method="post" name="frmProduct" id="frmProduct">
      
        <table width="600" height="327" border="1" cellpadding="0" cellspacing="0" bgcolor="#d7c81b">
		  <p>&nbsp;</p>
		  <tr>
		  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="frmSelect" id="frmSelect" >
            <td height="46"><span class="style3">Category Id</span></td>
            <td class="style3">
				  <select name="catId">
                    	<?php
				  //get category id from the database 
			$sql = "SELECT cat_id FROM tbl_category ORDER BY cat_id";
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
			{
			  echo "<option value=\"".$row['cat_id']."\">".$row['cat_id']."</option> \n  ";
			}
			
			?>				    
			      </select>
			      <input type="submit" name="btnSelectId" id="btnSelectId" value="OK"/ >
			</td>
		</form>
		</tr>
          <tr>
            <td height="45" class="style3">Category Name</td>
            <td class="style3"><p>
              <input name="txtName" type="text"  size="50" maxlength="100" value="<?php if(isset($_POST["txtName"])){echo $name;} ?>" />
            </p>
              <p>&nbsp; </p></td>
          </tr>
          <tr>
            <td height="44" class="style3">Description</td>
            <td class="style3"><p>
              <textarea name="catDesc" cols="50" rows="4"><?php if(isset($_POST["catDesc"])){echo $description;} ?>
            </textarea>
            </p>
              <p>&nbsp; </p></td>
          </tr>
          <tr>
            <td height="53" class="style3"><img src="<?php echo "../../admin/category/upload/" . $image; ?>"> </td>
            <td class="style3"><p>
              <input name="deleteImg" type="submit"  value="Delete Current Image" onClick="return Delete();"/>
            </p>
              <p>&nbsp; </p></td>
          </tr>
          <tr>
            <td height="52" class="style3">New Image</td>
            <td><input name="file" type="file" id="file" /></td>
          </tr>
        </table>
        <p>&nbsp;</p>
        <p>
          <input name="btnModifyCategory" type="submit"  value="Modify" onClick="return Modify();"/>
          <input name="btnCancel" type="button"  value="Cancel" onclick="window.location.href='../categories.php'" />
          <input name="btnBack" type="button"  value="Back" onclick="window.location.href='../categories.php'"/ >
        </p>
        <p>&nbsp;</p>
		</form>
</div>
      <p align="center">&nbsp;&nbsp;&nbsp;&nbsp;</p>
</body>
</html>

on lines 44 and 49 you have: UPDATE tbl_category SET cat_name ='$name2' WHERE cat_id = '$name' If I am not mistaken it should be ... WHERE cat_id=$cid Also, on line 10 put the following: $cid = isset($_POST['catId']) ? $_POST['catId'] : 0;

and On line 14 you are using $cid for $_POST;
and again on 29 same variable is used (isset($_POST))
$cid = $_POST;
is it working correctly???

and On line 14 you are using $cid for $_POST;
and again on 29 same variable is used (isset($_POST))
$cid = $_POST;
is it working correctly???

What's your point? He is consistently setting the value of $cid to $_POST. If anything, it is redundant. Hence my suggestion to put $cid = isset($_POST['catId']) ? $_POST['catId'] : 0; on line 10. That way he can get rid of lines 14 and 29.

From what I can see, the problem is not $catId, but not using the right variable in the UPDATE statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.