$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";
尝试使用字符串将行插入表中。继续得到语法错误。
当前错误是:
解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,期待T_STRING或T_VARIABLE或T_NUM_STRING
答案 0 :(得分:1)
你应该总是以下面的方式准备你的变量,这是一个很好的做法:
$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')";
答案 1 :(得分:1)
您正在混合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'] . "')";
警告: Please, don't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
答案 2 :(得分: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'] . "')";
或者你可以简单地用大括号({}
)包装它,如下所示:
$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']}')";
答案 3 :(得分: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] ')";
答案 4 :(得分: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'] ."')";
答案 5 :(得分: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'] . "')";