Jump to content

Insert query using Session values


Go to solution Solved by AaronClifford,

Recommended Posts

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 by Icewolf
Link to comment
https://forums.phpfreaks.com/topic/283778-insert-query-using-session-values/
Share on other sites

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 by Ch0cu3r

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 by Icewolf

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 by AaronClifford

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

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.