0

Hello to all,
Please tell me the error from the following code. When I choose a file from 'file' field, then it update the image, but when I dont choose a file. It doesn't keep the same image, i.e. the image get disappear. What is the problem in my 'If.....else code'.
Please, help me.

//edit-demo2.php

<?php
//session_start();
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('admin');
$pid = $_GET['id'];

$select= "SELECT * FROM gallery where id='$pid'";
$query = mysql_query($select);
//echo $rsprofiles;
$row = mysql_fetch_array($query);
//$a=$_POST['current'];
//echo "current file :".$a;
$submit=$_POST['submit'];
if(!empty($submit))
{
	$pid1=$_POST['pid'];
	$title=$_POST['title'];
	$file=time().$_FILES['file']['name'];
	if(!empty($file))
	{
		$file_new=move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload-img/" .$file);
      //echo "Stored in: " . "upload-img/" . $_FILES["file"]["name"];
	  $update=mysql_query("update gallery set title='$title',file='$file' where id='$pid1'");
	}
	else
	{
		$file_old=$_POST['current'];
     
	  $update=mysql_query("update gallery set title='$title',file='$file_old' where id='$pid1'");
	}
	header("Location: manage.php");
}
?>

<body>
<form action="edit-demo2.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="pid" id="pid" value="<?php echo $row['id']; ?>" />
  <p align="center">Title: <input type="text" name="title" id="title" value="<?php echo $row['title']; ?>" /></p>
  
<p align="center">Image :<input type="file" name="file" id="file" /><input type="text" name="current" id="current" value="<?php echo $row['file']; ?>" /><img src="upload-img/<?php echo $row['file']; ?>" height="110" width="110" /></p>


<p align="center">  <input type="submit" name="submit" id="submit" value="submit" /><a href="manage.php">Show</a></p>
</form>
</body>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Pooja J.
0

When you have not selected any file then '$_FILES' will not have any values. So in the line..

$file=time().$_FILES['file']['name'];

the $file will only have the value returned by time() function, hence the above statement will be equivalent to

$file=time();

Also please place your code within the

tags.

0

When I not choose any file. $file should contain the value of hidden file.i.e. current file

0

For that you need to check if you have selected a file or not. Try it like this .

if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != "") {
// File selected.
} else {
// No file selected.
}
0

see, wt happens actually.
The page showing the particular record, i.e. image's title and image. I want to to edit that information.
1) User may change the title only,
2) he may change the image only or
3) he may change both.
So, 2nd and 3rd condition work from my code. when user submits the form by changing title only, the present image should remain unchanged. for this, I took hidden field which holds the current image. i.e. when 'file' field is empty, the value of the hidden filed goes to the $file. here is the prob. actually (in else clause) .Prob. is that the value of the hidden field doesn't work properly, the image is dissappers when user only change the title.

Please, help me.

0

Assign the query to a variable like this and then echo that. Check if all the values are returned correctly and try to run this query manually using phpmyadmin.

$query = "update gallery set title='$title',file='$file_old' where id='$pid1'";
echo $query;
0

Thanks for suggestion.
But I tried it many times. It doesn't happened.

0

This prob is solved by my senior.

<?php
//session_start();
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('admin');
$pid = $_GET['id'];

$select= "SELECT * FROM gallery where id='$pid'";
$query = mysql_query($select);
//echo $rsprofiles;
$row = mysql_fetch_array($query);

//echo "current file :".$a;
$submit=$_POST['submit'];
if(!empty($submit))
{
	$pid1=$_POST['pid'];
	$title=$_POST['title'];
	$file=$_FILES['file']['name'];
	$a=$_POST['current'];
	if(!empty($file))
	{
		$file_new=move_uploaded_file($_FILES["file"]["tmp_name"], "upload-img/" .time().$file);
      //echo "Stored in: " . "upload-img/" . $_FILES["file"]["name"];
	  $update=mysql_query("update gallery set title='$title',file='$file' where id='$pid1'");
	}
	else
	{
		$file_old=$a;
     
	  $update=mysql_query("update gallery set title='$title',file='$file_old' where id='$pid1'");
	}
	header("Location: manage.php");
}
?>

Edited by nav33n: Use [code][/code] tags to wrap your code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.