Icewolf Posted November 10, 2013 Share Posted November 10, 2013 (edited) Hi I am trying to create a insert query that stores some session values. However I am getting an error and can't figure out what is causing it. I have tried to search for the error but can't find the answer. Here is the error. Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/pdogclan/public_html/community/addtobasket.php on line 6 Here is the code <?php include 'connect.php'; include 'timeout.php'; include 'header_signin.php'; $sql = ("INSERT INTO `shp_order_items`(`product_id`, `user_id`) VALUES ('.$_SESSION['prod_id'].' , '.$_SESSION['user_id'].')"); ?> Edited November 10, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/ Share on other sites More sharing options...
Ch0cu3r Posted November 10, 2013 Share Posted November 10, 2013 (edited) If you're using $_SESSION variables within "..." (double quoted string) then you need to wrap the session variable within curly braces $sql = "INSERT INTO `shp_order_items`(`product_id`, `user_id`) VALUES ('{$_SESSION['prod_id']}' , '{$_SESSION['user_id']}')"; Or use concatenation $sql = "INSERT INTO `shp_order_items`(`product_id`, `user_id`) VALUES ('".$_SESSION['prod_id']."' , '".$_SESSION['user_id']."')"; Edited November 10, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457767 Share on other sites More sharing options...
Icewolf Posted November 10, 2013 Author Share Posted November 10, 2013 (edited) Thank you Ch0cu3r. That was it.Now I just need to figure out why it is pulling the wrong product id. I need to figure out how to pull the product id of the line they are on now. Here is what I have. include 'connect.php'; include 'timeout.php'; include 'header_signin.php'; echo '<a class="item" href="redeemer.php">Redeemer</a>'; $sql = "SELECT * FROM shp_products"; $result = mysql_query($sql); if(!$result) { echo "There are no products."; } else{ echo '<table border="1"> <tr> <th>Image</th> <th>Product</th> <th>Description</th> <th># of Bank Points</th> </tr>'; while($row = mysql_fetch_assoc($result)) { echo '<tr>'; echo "<td><img src='images/".$row['prod_image']."'></td>"; echo '<td>'. $row['prod_name']. '</td>'; echo '<td>' . $row['prod_desc']. '</td>'; echo '<td>' . $row['prod_price']. '</td>'; echo '<td><a href="addtobasket.php?acion&id=1">Select Item</a></td>'; echo '</tr>'; $_SESSION['prod_id'] = $row['prod_id']; Edited November 10, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457770 Share on other sites More sharing options...
AaronClifford Posted November 10, 2013 Share Posted November 10, 2013 (edited) By the looks of it your setting the $_SESSION['prod_id'] in the while loop out putting your data. I'm guessing based on the limited code but this line: echo '<td><a href="addtobasket.php?acion&id=1">Select Item</a></td>'; Should be: echo '<td><a href="addtobasket.php?acion&id={$row['product_id']}">Select Item</a></td>'; However I'm not sure what "acion" is? Then in addtobasket.php it should be: $sql = "INSERT INTO `shp_order_items`(`product_id`, `user_id`) VALUES ('{$_GET['id']}' , '{$_SESSION['user_id']}')"; I'm sort of guessing based on the above code, but in theory it should work. Edited November 10, 2013 by AaronClifford Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457780 Share on other sites More sharing options...
Icewolf Posted November 10, 2013 Author Share Posted November 10, 2013 Thank you for looking but when I add echo echo '<td><a href="addtobasket.php?acion&id={$row['product_id']}">Select Item</a></td>'; I get this error now Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/pdogclan/public_html/community/shop.php on line 40 Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457787 Share on other sites More sharing options...
Solution AaronClifford Posted November 10, 2013 Solution Share Posted November 10, 2013 Try: echo '<td><a href="addtobasket.php?acion&id='.$row['product_id'].'">Select Item</a></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457789 Share on other sites More sharing options...
Icewolf Posted November 10, 2013 Author Share Posted November 10, 2013 Thanks Aaron that worked but the product id is still not being captured from that row. Now it is just pulling 0. Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457792 Share on other sites More sharing options...
AaronClifford Posted November 10, 2013 Share Posted November 10, 2013 Ah sorry what is the id field name for `shp_products`, replace the product_id with the id field from `shp_products` Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457796 Share on other sites More sharing options...
Icewolf Posted November 10, 2013 Author Share Posted November 10, 2013 (edited) that works it was prod_id thanks for your help Edited November 10, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457797 Share on other sites More sharing options...
AaronClifford Posted November 11, 2013 Share Posted November 11, 2013 No worries glad I could help! Quote Link to comment https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/#findComment-1457822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.