-1
$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount, company) VALUES (' . $_SESSION['user_id'] . ', '1', ' .    $row['product_id'] . ', ' . $saving . ', ' . $row['company'] . ')";

$retval = mysql_query( $sql, $connection );
    if(! $retval ){
        die('Could not enter data: ' . mysql_error());

}
        echo "Entered data successfully\n";

Trying to a string to insert a row into a table. Keep getting syntax errors.

Current error is:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

halfer
  • 19,471
  • 17
  • 87
  • 173
mos finn
  • 9
  • 5

6 Answers6

1

You should always prepare your variables in a good manner like below that is a good practice:

    $userId = $_SESSION['user_id'];
    $productId = $row['product_id'];
    $company = $row['saving'];
    $categoryId = 1;
    $savingAmount = $saving;

    $sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, 
    savings_amount, company) 
    VALUES ('$userId', 'categoryId', '$productId', '$savingAmount', '$company')";
Manish Jangir
  • 5,114
  • 4
  • 32
  • 69
1

You are mixing quotes,

$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount,   
        company) VALUES ('" . $_SESSION['user_id'] . "', '1', '" .    $row['product_id'] .  
        "', '" . $saving . "', '" . $row['company'] . "')";

Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Rikesh
  • 25,621
  • 14
  • 77
  • 86
1

You concatinated your string wrong. What you want is this:

$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount, company) VALUES ('" . $_SESSION['user_id'] ." ', '1', '" .    $row['product_id'] . "', '" . $saving . "', '" . $row['company'] . "')";

Alternatively you could simple wrap it in curly brackets ({}) like this:

$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount, company) VALUES ('{$_SESSION['user_id']} ', '1', '{$row['product_id']}', '$saving ', '{$row['company']}')";
Darren
  • 12,924
  • 4
  • 37
  • 76
0
$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount, company) VALUES (' $_SESSION[user_id] ', '1', ' $row[product_id] ', ' $saving ', ' $row[company] ')";
Mangesh Parte
  • 2,179
  • 1
  • 12
  • 16
0

Your query is not proper. try

$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, 
        savings_amount, company) VALUES ('". $_SESSION['user_id']."',1,
        '". $row['product_id'] ."', '". $saving . "', '". $row['company'] ."')";
Bhumi Shah
  • 8,909
  • 7
  • 57
  • 96
0

You have to use double quotes to end a string

$sql = "INSERT INTO user_saving_plans (user_id, catergory_id, product_id, savings_amount, company) VALUES ('" . $_SESSION['user_id'] . "', '1', '" .    $row['product_id'] . "', '" . $saving . "', '" . $row['company'] . "')";
Jens
  • 63,364
  • 15
  • 92
  • 104