Jump to content

Recommended Posts

Hi I was wondering if anyone could help me out with a code problem.

 

I am not very advanced with php but I do try to learn and I have been trying to figure out how to do something for a few hours now and im hoping someone here can help.

 

Basically I have a php form with an upload box that posts data to my database this all works perfectly but my problem now is editing my data.

 

When I edit the data and ignore the upload box it deletes the currect image in my database that I originally added. How can I edit the code so it keeps the currect image if the upload box is left blank.

 

This is my currect modify page code:

 

<?php
//This is the directory where images will be saved
$target = "images/products/";
$target = $target . basename( $_FILES['link_image']['name']);

//This gets all the other information from the form
$link_image=($_FILES['link_image']['name']);


//Writes the photo to the server
if(move_uploaded_file($_FILES['link_image']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

#+---------------------------------------
$short_desc				=	$_POST['short_desc'];
$url					        =	$_POST['url'];
$category				        =	$_POST['category'];
$designers				=	$_POST['designers'];
$shop					=	$_POST['shop'];
$price			   	        =	$_POST['price'];
$rss_long_desc			=	$_POST['rss_long_desc'];
$long_desc				=	$_POST['long_desc'];
$news_id				        =	$_POST['news_id'];
$todays_date			        =	date('Y-m-j H:i:s');
#+---------------------------------------


$modify_news_query		 = 	"UPDATE ";
$modify_news_query		.= 	$tbl_news;
$modify_news_query		.= 	" SET ";
$modify_news_query		.= 	" short_desc='" . $short_desc . "', ";
$modify_news_query		.= 	" rss_long_desc='" . $rss_long_desc . "', ";
$modify_news_query		.= 	" url='" . $url . "', ";
$modify_news_query		.= 	" date_added='" . $todays_date . "', ";
$modify_news_query		.= 	" link_image='" . $link_image . "', ";
$modify_news_query		.= 	" price='" . $price . "', ";
$modify_news_query		.= 	" category='" . $category . "', ";
$modify_news_query		.= 	" designers='" . $designers . "', ";
$modify_news_query		.= 	" shop='" . $shop . "' ";
$modify_news_query		.= 	" WHERE news_id='" . $news_id . "'";
$modify_news_query		.= 	";";

$modify_news_result 	= 	domysql($modify_news_query,'updating 1 news item in ' . $_SERVER['PHP_SELF']);

$action					=	'';
$message				=	"Updated Item : '" . $short_desc . "'";

//echo "Modifying news item ".$news_id." with query: ".$modify_news_query;
#+---------------------------------------
?>
<script type="text/javascript">
<!--
function x() {
window.location.href = "admin.php?news_action=preview&indexKey=<?php echo $news_id;?>"
}
x()
-->
</script>

Link to comment
https://forums.phpfreaks.com/topic/256218-upload-box-query/
Share on other sites

Hi I have changed my code to the following and when I modify the page and leave the upload box field blank it is still posting the empty data and removing the original file name from the database.

 

<?php
$link_image=($_FILES['link_image']['name']);

if(!empty($_FILES["link_image"]["name"])) {


$uploaddir = 'images/products/';
$uploadfile = $uploaddir . basename($_FILES['link_image']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['link_image']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

}

Link to comment
https://forums.phpfreaks.com/topic/256218-upload-box-query/#findComment-1313639
Share on other sites

Hi thank you for the reply. This is the mysql code:

 

<?php
$link_image=($_FILES['link_image']['name']);
if(!empty($_FILES["link_image"]["name"])) {

$uploaddir = 'images/products/';
$uploadfile = $uploaddir . basename($_FILES['link_image']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['link_image']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

}

#+---------------------------------------
$short_desc				=	$_POST['short_desc'];
$url					=	$_POST['url'];
$category				=	$_POST['category'];
$designers				=	$_POST['designers'];
$price			   	    =	$_POST['price'];
$long_desc				=	$_POST['long_desc'];
$products_id			=	$_POST['products_id'];
$todays_date			=	date('Y-m-j H:i:s');
#+---------------------------------------


$modify_products_query		 = 	"UPDATE ";
$modify_products_query		.= 	$tbl_products;
$modify_products_query		.= 	" SET ";
$modify_products_query		.= 	" short_desc='" . $short_desc . "', ";
$modify_products_query		.= 	" long_desc='" . $long_desc . "', ";
$modify_products_query		.= 	" url='" . $url . "', ";
$modify_products_query		.= 	" date_added='" . $todays_date . "', ";
$modify_products_query		.= 	" link_image='" . $link_image . "', ";
$modify_products_query		.= 	" price='" . $price . "', ";
$modify_products_query		.= 	" category='" . $category . "', ";
$modify_products_query		.= 	" designers='" . $designers . "'";
$modify_products_query		.= 	" WHERE products_id='" . $products_id . "'";
$modify_products_query		.= 	";";

$modify_products_result 	= 	domysql($modify_products_query,'updating 1 products item in ' . $_SERVER['PHP_SELF']);

$action					=	'';
$message				=	"Updated Item : '" . $short_desc . "'";

?>
<script type="text/javascript">
<!--
function x() {
window.location.href = "admin.php?products_action=preview&indexKey=<?php echo $products_id;?>"
}
x()
-->
</script>

 

 

And this is the upload box code for my modify page

 

<?php
$product_id = $_GET['product_id'];
$get_link_query  = "SELECT * FROM products";
$get_link_query .= ";";
$get_link_result = domysql($get_link_query,'selecting link to modify ' . $_SERVER['PHP_SELF']);

$row = mysql_fetch_assoc($get_link_result);
extract($row);
?>

<div id="admincontentcontainer">
<div align="center">
<br/>
<h1>Modify a Product Product </h1>
<a href="admin.php" class="mainmenulinks">Admin Main Menu</a> | 
<a href="logout.php" class="mainmenulinks">Log out of Admin</a>
</div>
<br/>
<img src='http://www.glitzyfashion.co.uk/images/divider.jpg' alt='Glitzy Fashion'  />
<br/>


<form enctype="multipart/form-data" name="modify_products_config" method="POST" action="admin.php">
<input name="products_action" type="hidden" id="products_action" value="modify_products_config">

<h3>Product Title</h3>
<input name="short_desc" id="short_desc" type="text" size="80" class="input" value="<?php echo $short_desc; ?>" />

<h3>Product Description</h3>
<textarea name="long_desc" id="long_desc" cols="80" rows='20' wrap='VIRTUAL' class='input'>
<?php echo $long_desc; ?>
</textarea>


<h3>Affiliate Url</h3>
<input name="url" id="url" type="text" size="50" class="input" value="<?php echo $url; ?>"/>
<h3>Current Image</h3>
<img src='/images/products/<?php echo $link_image; ?>' width='200'/>
  

<h3>Upload an Image</h3>
<input type="hidden" name="size" value="350000">
<input type="file" name="link_image">



<h3>Price</h3>
<input name="price" id="price" type="text" size="50" class="input" value="<?php echo $price; ?>"/>


<h3>Product Category</h3>
<select id="category" name="category" class="input" >
<option selected="true" value='<?php echo $category; ?>'><?php echo $category; ?> (current)</option>
<option value=''>--choose category--</option>
<option value='Brands'>Brands</option>
<option value='Accessories'>Accessories</option>
<option value='Bags and Purses'>Bags and Purses</option>
<option value='Beauty'>Beauty</option>
<option value='Coats and Jackets'>Coats and Jackets</option>
<option value='Evening Party Dresses'>Evening Party Dresses</option>
<option value='Denim'>Denim</option>
<option value='Trousers and Shorts'>Trousers and Shorts</option>
<option value='Jumpers and Cardigans'>Jumpers and Cardigans</option>
<option value='Tops'>Tops</option>
<option value='Shoes'>Shoes</option>
<option value='Childrens'>Childrens</option>
<option value='Gifts'>Gifts</option>
<option value='Homeware'>Homeware</option>
</select>

<h3>Choose a Designer</h3>
<select id="designers" name="designers" class="input" >
<option selected="true" value='<?php echo $designers; ?>'><?php echo $designers; ?> (current)</option>
<option value=''>--choose a designer--</option>
<?php 
$query = mysql_query("SELECT * FROM shop where linkType='brands' ORDER BY h1");
while($row = mysql_fetch_assoc($query)) {
extract($row);	?>
<option value='<?php echo $h1; ?>'><?php echo $h1; ?></option>
<?php } ?>
</select>
<br/><br/>
<input name="products_id" type="hidden" id="products_id" value="<?php echo $products_id;?>">
<input type="submit" name="submit"  value="Modify Products" tabindex="2">
</form>

</div>

 

 

Link to comment
https://forums.phpfreaks.com/topic/256218-upload-box-query/#findComment-1314078
Share on other sites

Thank you for your help the form now remembers the image however it wont let me update the image if I needed to change it. I'm not sure how to do change it.

 

This is the updated code:

 

<?php
$link_image=($_FILES['link_image']['name']);
if(!empty($_FILES["link_image"]["name"])) {

$uploaddir = 'images/products/';
$uploadfile = $uploaddir . basename($_FILES['link_image']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['link_image']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

}
#+---------------------------------------
$short_desc				=	$_POST['short_desc'];
$url					        =	$_POST['url'];
$category				        =	$_POST['category'];
$designers				=	$_POST['designers'];
$price			   	        =	$_POST['price'];
$long_desc				=	$_POST['long_desc'];
$link_image				=	$_POST['link_image'];
$products_id			        =	$_POST['products_id'];
$todays_date			        =	date('Y-m-j H:i:s');
#+---------------------------------------

$modify_products_query		 = 	"UPDATE ";
$modify_products_query		.= 	$tbl_products;
$modify_products_query		.= 	" SET ";
$modify_products_query		.= 	" short_desc='" . $short_desc . "', ";
$modify_products_query		.= 	" long_desc='" . $long_desc . "', ";
$modify_products_query		.= 	" url='" . $url . "', ";
$modify_products_query		.= 	" date_added='" . $todays_date . "', ";
$modify_products_query		.= 	" price='" . $price . "', ";
$modify_products_query		.= 	" category='" . $category . "', ";
$modify_products_query		.= 	" designers='" . $designers . "' ";

if($link_image <> '') $modify_products_query      .=    " link_image='" . $link_image . "' ";

$modify_products_query		.= 	" WHERE products_id='" . $products_id . "'";
$modify_products_query		.= 	";";

$modify_products_result 	= 	domysql($modify_products_query,'updating 1 products item in ' . $_SERVER['PHP_SELF']);

$action					=	'';
$message				=	"Updated Item : '" . $short_desc . "'";

?>
<script type="text/javascript">
<!--
function x() {
window.location.href = "admin.php?products_action=preview&indexKey=<?php echo $products_id;?>"
}
x()
-->
</script>

Link to comment
https://forums.phpfreaks.com/topic/256218-upload-box-query/#findComment-1314151
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.