In the following function, to modify a product in a shopping cart,

function modifyProduct()
	$productId   = (int)$_GET['productId'];	
    $catId       = $_POST['cboCategory'];
    $name        = $_POST['txtName'];
	$description = $_POST['mtxDescription'];
	$price       = str_replace(',', '', $_POST['txtPrice']);
	$qty         = $_POST['txtQty'];
	$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');

	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];
 //echo $thumbnail;

	// if uploading a new image
	// remove old image
	if ($mainImage != '') {
		$mainImage = "'$mainImage'";
		$thumbnail = "'$thumbnail'";

	} else {
		// if we're not updating the image
		// make sure the old path remain the same
		// in the database
		$mainImage = 'pd_image';
		$thumbnail = 'pd_thumbnail';
	$sql   = "UPDATE tbl_product 
	          SET cat_id = $catId, pd_name = '$name', pd_description = '$description', pd_price = $price, 
			      pd_qty = $qty, pd_image = $mainImage, pd_thumbnail = $thumbnail
			  WHERE pd_id = $productId";  

	$result = dbQuery($sql);
	header('Location: index.php');			  

At the following lines of code,

$mainImage = "'$mainImage'";
$thumbnail = "'$thumbnail'";

why $mainimage is inside double quotes ?

Using double quotes in PHP has many advantages, not the least of which is that all variables will be evaluated inside them. Consider the following code:

for($i=0; $i<10; $i++)
   echo "$i ";

This will output:
0 1 2 3 4 5 6 7 8 9
The following code:

for($i=0; $i<10; $i++)
   echo '$i '; // notice the single quotes

outputs the following:
$i $i $i $i $i $i $i $i $i $i
There are other advantages to using double quotes in php, including being able to use escape characters such as \n for a new line in plain text.

EDIT: Now in your specific example, The variables are inside double quotes and single quotes, because the single quotes are needed in the SQL statement but we need to evaluate the value of the variable.


in the following lines of codes at the above posting,

// if we're not updating the image
		// make sure the old path remain the same
		// in the database
		$mainImage = 'pd_image';
		$thumbnail = 'pd_thumbnail';

how we can understand that the 'pd_image' and 'pd_thumbnail'
which is getting updated belongs to the corresponding productid(i.e $productid)?

$productId = (int)$_GET;

and you have the SQL Condition:

WHERE pd_id = $productId

so lets say $_GET = 2;
it means $productId = 2;

the query:
$sql = "UPDATE tbl_product SET cat_id = $catId, .... WHERE pd_id = $productId";

$sql = "UPDATE tbl_product SET cat_id = $catId, .... WHERE pd_id = 2";


hi :)
yes clear! thanks a lot!

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.