-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,824
  • 17
  • 99
  • 186
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,329
  • 4
  • 42
  • 75
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
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
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
  • 13,050
  • 4
  • 41
  • 79
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
  • 13
  • 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
  • 9,323
  • 7
  • 63
  • 104
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
  • 67,715
  • 15
  • 98
  • 113