2013-10-14 34 views
0

我知道這非常簡單,幾年來我沒有碰過PHP和SQL。基本插入SQL

這工作

$ mysqli->查詢(「INSERT INTO sonyCES2013registrationidfirstNamelastNameeMailtelephoneoutletcomfirmpreferTimedate)VALUES(NULL,Cat ,'Catherine', '[email protected]','123-456-4561','Some Text','Yes','4:00 pm' ,'1/09/14')「);

這不起作用

$ mysqli->查詢(「INSERT INTO sonyCES2013registrationidfirstNamelastNameeMailtelephoneoutletcomfirmpreferTimedate) VALUES (NULL,{$fName}{$lName}{$eMail}{$telephone}{$outlet}{$comfirmation},{ $preferTime},{$day})「);

的幫助,是我沒有檢查變量不是空的,我也嘗試沒有``之間的每個{}

+3

始終使用綁定變量。 – Flimzy

+3

您需要添加引號。 – Aneri

+1

你需要在字符串周圍添加引號...''{$ fname}',...'但是它看起來像你真的打開了自己的SQL注入 –

回答

1

您需要添加'的變量,以使interpeter能明白你正在嘗試做的(在這種情況下,通過一些PHP變量作爲參數)

利用這一點,看看它是否工作:

$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName, 
       eMail, telephone, outlet, comfirm, preferTime, date) VALUES 
       (NULL,'$fName','$lName','$eMail','$telephone','$outlet', 
       '$comfirmation','$preferTime','$day')"); 
1

它不會按照這種方式,因爲產生的SQL看起來是這樣的:

$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName, eMail, telephone, outlet, comfirm, preferTime, date) VALUES (NULL,Cat, Catherine, [email protected], 123-456-4561, Some Text, Yes, 4:00pm ,1/09/14)"); 

請注意缺少引起字符串的單引號,導致SQL無效。