为什么我的SQL查询失败?

时间:2014-04-18 20:01:07

标签: php sql

我测试了与SQL的连接,并且正确连接。出于某种原因,我的查询返回了Error. BOOM!消息,但它们无法正常工作。知道发生了什么事吗?

$query = "INSERT INTO `members` 
(`id`,`fname`,`lname`,`password`,`email`,`vatid`,`transfer`, `link`, `ip`) 
SELECT `id`,`fname`,`lname`,`password`,`email`,`vatid`,`transfer`, `link`, `ip` 
FROM applicants WHERE id=$_GET[id]";

if ( !mysqli_query($connection,$query) ){
   // 
   //die (mysql_error());
   print("Error. BOOM!");
} else {
   //Or on success:
   print ("Thank you for your registration");
   //or you can do redirect to some page, like this:

  //header('location: /thanks.php');
}

$query = "DELETE * FROM applicants WHERE id=$_GET[id]";

if ( !mysqli_query($connection,$query) ){
   // 
   //die (mysql_error());
   print("Error. BOOM!");
} else {
   //Or on success:
   print ("Thank you for your registration");
   //or you can do redirect to some page, like this:

  //header('location: /thanks.php');
}

5 个答案:

答案 0 :(得分:1)

首先是先决条件,请切换到参数化查询。我不是一个PHP人,但我认为$_GET[id]只是一个请求参数

INSERT INTO `members` (
    `id`
    ,`fname`
    ,`lname`
    ,`password`
    ,`email`
    ,`vatid`
    ,`transfer`
    ,`link`
    ,`ip`
    )
SELECT `id`
    ,`fname`
    ,`lname`
    ,`password`
    ,`email`
    ,`vatid`
    ,`transfer`
    ,`link`
    ,`ip`
FROM applicants
WHERE id = ?

我的第一个猜测是成员中的id是生成的列;我不知道你正在使用什么RDBMS,但你可能不得不让引擎决定新成员的id是什么。

答案 1 :(得分:0)

首先,您有SQL注入问题。在将变量放入查询之前,始终将变量转义。

使用它来检查错误并获得有关正在发生的事情的更好信息:

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

第二个查询也有拼写错误,请改用:

$query = "DELETE * FROM applicants WHERE id=$_GET['id']";

答案 2 :(得分:0)

您可以尝试使用此格式进行删除查询以确定错误:

<?php
$sql = " 
    DELETE FROM 
        applicants
    WHERE 
        id = ".mysql_real_escape_string($_GET[id])."
";

if(!$res = mysql_query($sql)) 
{ 
    trigger_error(mysql_error().'<br />In query: '.$sql); 
} 
elseif(mysql_affected_rows() == 0) 
{ 
    echo 'No records Deleted. <br />Query: '.$sql; 
} 
else 
{ 
    echo 'There are '.mysql_affected_rows().' records deleted.'; 
}
?>

答案 3 :(得分:0)

此:

DELETE * FROM applicants WHERE id=$_GET[id];

无效的sql。你必须丢失星号。

DELETE FROM applicants WHERE id=$_GET[id];

除了使调试变得困难的其他因素之外,如果任一查询失败,您将获得相同的输出。

答案 4 :(得分:-1)

error_reporting(E_ALL);

$query = "INSERT INTO `members` (`id`,`fname`,`lname`,`password`,`email`,`vatid`,`transfer`, `link`, `ip`) SELECT `id`,`fname`,`lname`,`password`,`email`,`vatid`,`transfer`, `link`, `ip` FROM applicants WHERE id=".$_GET['id']."";

if ( !mysqli_query($connection,$query) ){
   // 
 //die (mysql_error());
 print("Error. BOOM!");
} else {
   //Or on success:
   print ("Thank you for your registration");
   //or you can do redirect to some page, like this:

  //header('location: /thanks.php');
}

$query = "DELETE FROM applicants WHERE id=".$_GET['id']."";

if ( !mysqli_query($connection,$query) ){
   // 
   //die (mysql_error());
   print("Error. BOOM!");
} else {
   //Or on success:
   print ("Thank you for your registration");
   //or you can do redirect to some page, like this:

  //header('location: /thanks.php');
}

或者您可以在 sql 标签中的数据库中的phpmyadmin中测试您的查询。

或者在你的php文件中:

error_reporting(E_ALL);