2016-11-21 27 views
-1

我正在學習如何使用PHP與MySQLi和我有問題正確插入信息到我設置的表。這裏是我的代碼:PHP插入SQL表的錯誤與語法

$temp_category = $_POST['category']; 
    $temp_fname = $_POST['recipient-fname']; 
    $temp_lname = $_POST['recipient-lname']; 
    $temp_email = $_POST['recipient-email']; 
    $temp_date = $_POST['award-date']; 
    $auth_userId = $_SESSION['id']; 
    $auth_email = $_SESSION['email']; 


    $mysqli = mysqli_connect($host, $dbUser, $dbPword, $db, $port); 
    if ($mysqli->connect_errno) { 
     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
    } 

    $sql = 'INSERT INTO award_entry (award_category,granted_time,authorizing_user_id,authorizing_user_email,fname,lname,recipient_email), 
    VALUES ("'.$temp_category.'", "'.$temp_date.'","'.$auth_userId.'", "'.$auth_email.'","'.$temp_fname.'", "'.$temp_lname.'","'.$temp_email.'")'; 


    if ($mysqli->query($sql) === TRUE) { 
     echo "award_entry successfully updated"; 
    } 
    else { 
     echo "Error: " . $sql . "<br>" . $mysqli->error; 
    } 

我不斷收到錯誤你有一個錯誤「,在您的SQL語法;請查看對應於你的MySQL服務器版本使用附近的「VALUES(正確的語法手冊‘EmployeeWeek’ 「2016年11月5日」, 「20」, 「[email protected]」, 「傑克」,」第1" 行

,但我看不出有什麼錯我的代碼。

+2

'recipient_email),'<<< ahem。 –

+0

... *關 - (咳嗽)話題* - 錯字 –

+0

並且使用準備好的語句;你在進行SQL注入。 –

回答

0
$sql = 'INSERT INTO award_entry (award_category,granted_time,authorizing_user_id,authorizing_user_email,fname,lname,recipient_email)***,*** 
VALUES ("'.$temp_category.'", "'.$temp_date.'","'.$auth_userId.'", "'.$auth_email.'","'.$temp_fname.'", "'.$temp_lname.'","'.$temp_email.'")'; 

在VALUES之前的右括號之後擺脫逗號