我能';t使用php将行插入数据库
我正在尝试保存到数据库行。但我总是犯错误 我的代码是:我能';t使用php将行插入数据库,php,mysql,Php,Mysql,我正在尝试保存到数据库行。但我总是犯错误 我的代码是: function save_activation_key($connection, $username, $key) { $date = time(); $is_used = 0; $query = "INSERT INTO account_activation_key ( key, date, is_used) VALUES ( '" . $username . "',"
function save_activation_key($connection, $username, $key) {
$date = time();
$is_used = 0;
$query = "INSERT INTO account_activation_key ( key, date, is_used)
VALUES ( '" . $username . "',"
. " '" . $date . "',"
. " '" . $is_used . "')";
$retval = mysql_query($query, $connection);
echo $retval;
$retval = mysql_query( $query, $connection );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
}
在中,$connection
是到数据库的有效连接
数据库结构:
id : int
key: varcha(45)
date: date
is_used: tinyint(1)
当我调用我的代码时,我得到一个错误:
Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, date, is_used) VALUES ( 'uzivatelsky_jmeno', '1459971829', '0')' at line 1
哪里有问题
感谢您的帮助您的查询可能在将整数作为字符串(如字符串)的位置包含错误 “$username。”,” .““$日期。”,” ““.”使用了$ 应该是: “$username.”、“.”$日期。“,”。“$已使用。” 整数不应包含单个qoutes“
也许这是个错误
key
是MYSQL保留字,不应该真正用作列名
MYSQL保留字列表可以在这里找到
但是,如果您将这些列名用反勾号括起来,您就可以不受影响
您还可以简化查询字符串连接,如下所示,这使得调试更加容易
function save_activation_key($connection, $username, $key) {
$date = time();
$is_used = 0;
$query = "INSERT INTO `account_activation_key`
( `key`, `date`, `is_used`)
VALUES ( '$username', '$date', '$is_used')";
$retval = mysql_query($query, $connection);
echo $retval;
$retval = mysql_query( $query, $connection );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
}
大钞
请不要用它
已弃用(在PHP7中永远消失),这意味着当所有可用的都是PHP7或更高版本时,此代码将永远不会运行。
特别是如果你只是在学习PHP,那么就花点精力学习PDO
或mysqli_u979;
数据库扩展,
您可以
echo
您的查询语句并检查它是否有效吗?请不要使用,它已被弃用(在PHP7中已永远消失),特别是如果您只是学习PHP,请花费精力学习PDO
或mysqli
数据库扩展,只有键
是Smokey而不是日期
。我就知道你会插嘴说那句话。Arternoon Ralph@Fred ii午后SmokeyIt是或曾经是,但MYSQL为日期提供了解决方案,所以我知道你是正确的,但我认为最好不要使用它们,这是很久以前的事了;-)