任何人都知道为什么这个MYSQL插入不起作用


Anyone know why this MYSQL INSERT doesn't work?

$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("pilot", $con);
$sql = "INSERT INTO logs (id, userid, date, plane, from, to, blocksoff, takeoff,
landing, blockson, flighttime, traveltime, tachobefore, tachoafter, tacho, 
hobbsbefore, hobbsafter, hobbs, landings) VALUES ('$nfid', '$nfuserid', 
'$nfdate', '$nfplane', '$nffrom', '$nfto', '$nfblocksoff', '$nftakeoff', 
'$nflanding', '$nfblockson', '$nfflighttime', '$nftraveltime', '$nftachobefore', 
'$nftachoafter', '$nftacho', '$nfhobbsbefore', '$nfhobbsafter', '$nfhobbs',
'$nflandings')";
mysql_query($sql);

$sql没有错,似乎它只是不会查询.. :(

id|userid=int(11)  
date=date  
plane|from|to=text  
blocksoff|takeoff|landing|blockson=time  
flighttime|traveltime|tachobefore|tachoafter|tacho|hobbsbefore|hobbsafter|hobbs|landings=double

所有 $ 变量都来自文本框(如果重要的话)

可能是一些列名是MySql保留字(特别是fromto)。请逃离他们。

INSERT INTO logs (`id`, userid, date, plane, `from`, `to` ...)

您应该始终检查错误:

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

有点开放式的问题....

是否有任何变量返回 NULL 值? 如果尝试将 NULL 插入数据库,并且数据库列未设置为接受 NULL 值,则可能会导致错误。

您需要查看查询的实际操作。 如果文本框中有任何单引号或其他无效字符,这可能会搞砸您。

另外,为了您自己的个人改进,请查看PDO。 它可以帮助您通过使用预准备语句编写更安全的查询。http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/