-1
<html>
<head>
HTML CODE
<?
$username="xxxxxx";
$password="xxxxxx";
$database="xxxxxx";

mysql_connect(localhost,$username,$password);

$escape = "INSERT INTO monster VALUES ('',$_POST["name"],$_POST["soort"])";
$escape2 = "DELETE monster FROM monster LEFT OUTER JOIN (
            SELECT MIN( ID ) AS ID, NAME, PREF
            FROM monster
            GROUP BY NAME, PREF
            ) AS KeepRows ON monster.ID = KeepRows.ID
            WHERE KeepRows.ID IS NULL";

$query=mysql_real_escape_string($escape);
$query2=mysql_real_escape_string($escape2);

@mysql_select_db($database) or die("MySQL error: Kan inte ansluta till databasen.");
mysql_close();
?>
</body>
</html>

每次我运行这个(从另一个文件,包含名称和排序帖子)时,我都会收到 500 内部服务器错误。首先,我认为查询可能是问题所在,但它们甚至没有被执行。但是,我试图逃避查询。但还是报错。这段代码有什么问题?(注意:$escape2 是我发现的一些代码,可以删除数据库中的重复项。但我真的不知道如何格式化它以便可以通过 php 使用它。)

4

5 回答 5

0

problem in insert into statement

it should be

$escape = "INSERT INTO monster VALUES ('',".$_POST['name'].",".$_POST['soort'].")";

it is preferable to write colums name while writing insert queries

if column contains string values like VARCHAR or TEXT then use quoted_printable_decode

pass null if column is autoincrement

insert statment

$escape = "INSERT INTO monster (col1, col2, col3) VALUES (NULL,'".$_POST['name']."',".$_POST['soort'].")";

or

$escape = "INSERT INTO monster (col2, col3) VALUES ('".$_POST['name']."',".$_POST['soort'].")";
于 2013-10-24T05:09:46.153 回答
0

Use something like below...

$query = "INSERT INTO monster VALUES ('', '".$_POST["name"]."', '".$_POST["soort"]."')";

Please do not insert values without escaping.

于 2013-10-24T05:10:40.190 回答
0

尝试这个:

Whenever you insert string type of values in the database using query it has to pass in the quote format. So you just need to change your insert query here.

$query = "INSERT INTO monster VALUES ('', '".$_POST["name"]."', '".$_POST["soort"]."')";

write query like this.

- 谢谢

于 2013-10-24T09:17:07.770 回答
0

看起来你需要这样的东西:

$query = "INSERT INTO monster VALUES ('', '".$_POST["name"]."', '".$_POST["soort"]."')";

另外我建议使用准备好的语句,因为构建查询是不好的体验。

于 2013-10-24T05:34:24.607 回答
0

首先,我有一个很酷的提议给你。你对一些高级 PHP 有什么看法?向安全的 PHP + MySQL 应用程序的伟大世界迈进了一步?

向您介绍PDO。(我知道这不是您问题的答案,但您可以考虑)。查询的使用示例:

$db = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);

$insertQuery = $db->prepare('INSERT INTO monster VALUES ("", :name, :soort)');
$deleteQuery = $db->prepare('DELETE monster FROM monster LEFT OUTER JOIN (
SELECT MIN( ID ) AS ID, NAME, PREF
FROM monster
GROUP BY NAME, PREF
) AS KeepRows ON monster.ID = KeepRows.ID
WHERE KeepRows.ID IS NULL');

//to execute query:
$deleteQuery->execute();
//or with params:
$insertQuery->execute(array(
  ':name'  => $_POST['name'],
  ':soort' => $_POST['soort'],
));

酷吧?还有更多......现在根据您的问题,它可能是一切(因为我们没有错误日志)但我的猜测是:

  • 尝试使用<?php而不是<?
  • $escape = "INSERT INTO monster VALUES ('',{$_POST["name"]},{$_POST["soort"]})";

编辑: 正如您提供的错误日志 - 现在我确定问题出在$escape查询中。这是因为您使用了$escape = "<- 然后$_POST["name"]发生了“冲突”(如果我可以这么说的话)。

于 2013-10-24T05:39:30.270 回答