vinpkl Posted October 31, 2008 Share Posted October 31, 2008 hi all i m inserting data from my product table into cart table and getting this error Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\xampp\htdocs\vineet\shopping_cart.php on line 13 This is my complete code <?php require_once("config.php"); $_SESSION['product_id']=$_REQUEST['product_id']; $_SESSION['product_id']=$_REQUEST['product_id']; $id=$_SESSION['product_id']; //echo test; //exit; $qry="select product_id,image,product_name,price,shipping_cost from product_table where product_id=". $_SESSION['product_id']; //echo $qry; //exit; $result=mysql_query($qry); $row=mysql_fetch_array($result); $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values($id,$row['image'],$row['product_name'],1,$row['price'],$row['shipping_cost'],$row['price']+$row['shipping_cost'])"; echo $qry; exit; mysql_query($qry); ?> This is line no.13 $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values($id,$row['image'],$row['product_name'],1,$row['price'],$row['shipping_cost'],$row['price']+$row['shipping_cost'])"; what is causing error in my code. vineet Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/ Share on other sites More sharing options...
n3ightjay Posted October 31, 2008 Share Posted October 31, 2008 try ... row13 values(".$id.",".$row['image']."," ....... Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679046 Share on other sites More sharing options...
zenag Posted October 31, 2008 Share Posted October 31, 2008 use single quotes for values values('$id','$row[image]','$row[product_name]',1,'$row[price]' Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679047 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 try ... row13 values(".$id.",".$row['image']."," ....... hi when i used this code <?php $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values(".$id.",".$row['image'].",".$row['product_name'].",".1.",".$row['price'].",".$row['shipping_cost'].",".$row['price']."+".$row['shipping_cost'].")"; ?> then i received Parse error: syntax error, unexpected T_DNUMBER in E:\xampp\htdocs\vineet\shopping_cart.php on line 13. also in concatination of ".1." one dot is red and one is blue vineet Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679054 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 use single quotes for values values('$id','$row[image]','$row[product_name]',1,'$row[price]' hi zenag when i used this code <?php $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values('$id','$row[image]','$row[product_name]',1,'$row[price]','$row[shipping_cost]','$row[price]'+'$row[shipping_cost]')"; ?> then i received this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\xampp\htdocs\vineet\shopping_cart.php on line 12 insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values('','','',1,'','',''+'') Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679057 Share on other sites More sharing options...
n3ightjay Posted October 31, 2008 Share Posted October 31, 2008 you have to remember to enQuote your string/charactor values with quotes and leave you numeric values open <?php $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values(".$id.",'".$row['image']."','".$row['product_name']."',1,".$row['price'].",".$row['shipping_cost'].",".$row['price']."+".$row['shipping_cost'].")"; ?> i think thats it im not sure of your db structure though also if your hardcoding your quantity value don't quote it out Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679061 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 you have to remember to enQuote your string/charactor values with quotes and leave you numeric values open <?php $qry="insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values(".$id.",'".$row['image']."','".$row['product_name']."',1,".$row['price'].",".$row['shipping_cost'].",".$row['price']."+".$row['shipping_cost'].")"; ?> i think thats it im not sure of your db structure though also if your hardcoding your quantity value don't quote it out Hi n3ightjay i used it and received this Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\xampp\htdocs\vineet\shopping_cart.php on line 12 insert into cart_table(product_id,image,product_name,quantity,price,shipping_cost,total_cost) values(,'','',1,,,+) this is line 12 $row=mysql_fetch_array($result); Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679062 Share on other sites More sharing options...
n3ightjay Posted October 31, 2008 Share Posted October 31, 2008 okay what are the data types of cart_table( product_id image product_name quantity price shipping_cost total_cost Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679064 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 okay what are the data types of cart_table( product_id image product_name quantity price shipping_cost total_cost hi here is the datatypes in cart table cart_id = int(10) autoincrement product_id=int(20) image = varchar(40) product_name = varchar(40) price = float(9,2) quantity = int(10) shipping_cost = float(9,2) total_cost = float(9,2) vineet Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679068 Share on other sites More sharing options...
redarrow Posted October 31, 2008 Share Posted October 31, 2008 Try this please........ <?php session_start(); require_once("config.php"); // if the session_start() in here dont use it, use it on it own each page... $_SESSION['product_id']=$_GET['product_id']; // use get or post only request no good get from url or post from form. $id=$_SESSION['product_id']; $sql="SELECT `product_id`,`image`,`product_name`,`price`,`shipping_cost` FROM `product_table` WHERE `product_id`='$id'"; $result=mysql_query($qry)or die (mysql_error()); //add the error function. while(mysql_fetch_assoc($result)){ $qry="INSERT INTO `cart_table`(`product_id`,`image`,`product_name`,`quantity`,`price`,`shipping_cost`,`total_cost`) VALUES('$id','".$row['image']."','".$row['product_name']."',1,'".$row['price']."','".$row['shipping_cost']."' ,'".$row['price']+$row['shipping_cost']."')"; $result2=mysql_query($qry)or die(mysql_error());//add error function } ?> Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679079 Share on other sites More sharing options...
redarrow Posted October 31, 2008 Share Posted October 31, 2008 A pro's way...... <?php session_start(); require_once("config.php"); // the session_start(0 in here dont use it on it own each page... $_SESSION['product_id']=$_GET['product_id']; // use get or post only request no good $id=$_SESSION['product_id']; $sql="SELECT `product_id`,`image`,`product_name`,`price`,`shipping_cost` FROM `product_table` WHERE `product_id`='$id'"; $result=mysql_query($qry)or die (mysql_error()); //add the error function. while(mysql_fetch_assoc($result)){ $image=$row['image']; $product_name=$row['product_name']; $price=$row['price']; $shipping_cost=$row['shipping_cost']; $price=$row['price']+$row['shipping_cost']; $image=mysql_real-escape_string($_POST['image']); $product_name=mysql_real_escape_string($_POST['product_name']); $price=mysql_real_escape_string($_POST['price']); $shipping_cost=mysql_real_escape_string($_POST['shipping_cost']); $price=mysql_real_escape-string($row['price']+$row['shipping_cost']); $price=$_POST['price']; $qry="INSERT INTO `cart_table`(`product_id`,`image`,`product_name`,`quantity`,`price`,`shipping_cost`,`total_cost`) VALUES('$id','$image,'$product_name',1,'$price','$shipping_cost' ,'$price)"; $result2=mysql_query($qry)or die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679083 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 Try this please........ <?php session_start(); require_once("config.php"); // if the session_start() in here dont use it, use it on it own each page... $_SESSION['product_id']=$_GET['product_id']; // use get or post only request no good get from url or post from form. $id=$_SESSION['product_id']; $sql="SELECT `product_id`,`image`,`product_name`,`price`,`shipping_cost` FROM `product_table` WHERE `product_id`='$id'"; $result=mysql_query($qry)or die (mysql_error()); //add the error function. while(mysql_fetch_assoc($result)){ $qry="INSERT INTO `cart_table`(`product_id`,`image`,`product_name`,`quantity`,`price`,`shipping_cost`,`total_cost`) VALUES('$id','".$row['image']."','".$row['product_name']."',1,'".$row['price']."','".$row['shipping_cost']."' ,'".$row['price']+$row['shipping_cost']."')"; $result2=mysql_query($qry)or die(mysql_error());//add error function } ?> Hi redarrow thanks for the reply. it worked with no error. But gives the result as "query was empty". what does this means vineet Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679095 Share on other sites More sharing options...
redarrow Posted October 31, 2008 Share Posted October 31, 2008 did all the info get entred into the database m8.... cheek the id ok Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679098 Share on other sites More sharing options...
vinpkl Posted October 31, 2008 Author Share Posted October 31, 2008 did all the info get entred into the database mate.... cheek the id ok hi redarrow nothing got inserted into the cart table. also tell where we have writen $sql should it be $qry= $sql="SELECT `product_id`,`image`,`product_name`,`price`,`shipping_cost` FROM `product_table` WHERE `product_id`='$id'"; because in the bottom we are writing $qry=. also this is the code i m writing on my add to cart button. <?php echo "<a href=shopping_cart.php?id=" . $row['product_id'] . ">" ."<img src='images/add_cart.gif' border=0 />" . "</a>" . "</td>"; ?> vineet Link to comment https://forums.phpfreaks.com/topic/130830-error-inserting-data-from-product-table-into-cart-table/#findComment-679105 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.