我想用PHP編寫一個API,它將檢查用戶輸入的值,如果trackid和userid已經在數據庫中,它將更新評級。但是,如果曲目和用戶標識不存在,那麼API應該將它們與評分一起插入。如何才能做到這一點。請注意我不斷收到錯誤:更新或插入值的PHP代碼
Call to a member function rowCount() on a non-object
on line 14.任何幫助將不勝感激。
<?php
$id = $_GET['TrackId'];
$user = $_GET['UserId'];
$rating = $_GET['Rating'];
include("connect.php");
$query = "UPDATE `rating` SET `rating` = '$rating' WHERE track_id = '$id' AND user_id = '$user' AND rating_set=NOW()";
$sth = $dbc->query($query);
$rows = $sth->rowCount();
if ($rows != 2) {
$query = "INSERT INTO `rating` values ('$id','$rating','$user',NOW())";
$results = $dbc->query($query);
$rows = $results->rowCount();
header('Content-type: application/json; charset=utf-8');
echo $_GET['onJSONPLoad'];
echo "(" . json_encode($rows) . ")";
}
else
{
header('Content-type: application/json; charset=utf-8');
echo $_GET['onJSONPLoad'];
echo "(" . json_encode($rows) . ")";
}
$dbc = null;
?>
*旁註:*你的代碼受到SQL注入攻擊,你允許直接在查詢中插入GET值。 – Raptor
而是嘗試獲取'affected_rows' – zzlalani