我有一个最奇怪的问题.我写了这个非常简单的INSERT查询:
if(isset($_POST['putUser'])) {
$user = $_POST['user'];
$amount = $_POST['amount'];
$what = $_POST['what'];
$country = $_POST['country'];
$platform = $_POST['platform'];
$query = mysql_query('INSERT INTO sells(id, user, amount, what, country, platform) VALUES (NULL , '.$user.', 1, 1, 1, 1)');
if($query) {
echo 'ok';
} else {
die(mysql_error());
}
}
Run Code Online (Sandbox Code Playgroud)
它不起作用.它适用于我1在所有值中而不是像在示例中那样$user.但是当变量存在时,它会抛出错误Unknown column 'Test username' in 'field list'.哪里是我的错?
以及传递给查询的值未包含在引号内.当你没有在引号内包含一个字符串时,mysql假定它是一个字段名.你也忘了逃避你的绳子.
$query = mysql_query('INSERT INTO sells (id, user, amount, what, country, platform) '
. 'VALUES (NULL , "' . mysql_real_escape_string($user) . '", 1, 1, 1, 1)');
Run Code Online (Sandbox Code Playgroud)
最后,您应该远离使用mysql扩展并使用pdo或mysqli替代.
你将字段用户(字符串)作为非字符串插入尝试:
if(isset($_POST['putUser'])) {
$user = mysql_real_escape_string($_POST['user']);
$amount = $_POST['amount'];
$what = $_POST['what'];
$country = $_POST['country'];
$platform = $_POST['platform'];
$query = mysql_query('INSERT INTO sells(id, user, amount, what, country, platform) VALUES (NULL , "'.$user.'", 1, 1, 1, 1)');
if($query) {
echo 'ok';
} else {
die(mysql_error());
}
}
Run Code Online (Sandbox Code Playgroud)
正如大家对评论所说的那样,你不应该在mysql查询中直接使用post info,这使得"sql injection atack"成为世界上最容易的事情.你应该从字符串scape一些字符来防止这种情况.在PHP上做一些关于PDO的研究,这个链接在这里可能有所帮助
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |