2013-10-14 24 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无效。