0

hi
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 != '') {
		_deleteImage($productId);
		
		$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 ?

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by karthikanov24
0

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.

Edited by darkagn: Further explanation

0

hi

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)?

0

$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";

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

clear?

This topic has been dead for over six months. 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.