2017-02-16 142 views
0

我收到此错误,我需要一些帮助。 我一直被困在这个很长一段时间,我感觉好像问题在我的脸上。“字段列表”中的未知列'problem_id'

这是我的数据库结构。我使用的MySQL

mysql> describe user_supplied_problem_solution; 
+------------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+------------+------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| user_id | int(10) unsigned | NO | MUL | NULL |    | 
| problem_id | int(10) unsigned | NO | MUL | NULL |    | 
| solution | text    | NO |  | NULL |    | 
+------------+------------------+------+-----+---------+----------------+ 

这是运行时,我收到错误的PHP代码:

public function createSolution($description, $user_id, $part_order = NULL) { 
    $solution_id = NULL; 
    if(!isset($part_order)) { 
     print("running the problem level query\n"); // Debug 
     $query = "INSERT INTO user_supplied_problem_solution (problem_id, user_id, solution) 
       VALUES (:problem_id, :user_id, :solution)"; 
    } else { 
     print("running the part level query\n"); // Debug 
     $query = "INSERT INTO user_supplied_part_solution (user_id, part_id, solution) 
        VALUES (:user_id, :part_id, :solution)"; 
    } 
    $this->dbh->beginTransaction(); 
    try { 
     $statement = $this->dbh->prepare($query); 
     if(!isset($part_order)) { 
      print($query . PHP_EOL); // Debug 
      print("running the problem level binding\n"); // Debug 
      print("The problem ID is $this->problem_id\n"); // Debug 
      $statement->bindParam(':problem_id', $this->problem_id, PDO::PARAM_INT); 
     } else { 
      // Get the part_id 
      $part_id = $this->getPartIdFromOrder($part_order); 
      if(!$part_id) { 
       // Part_id does not exist -- rollback and return 
       $this->dbh->rollBack(); 
       return false; 
      } 
      $statement->bindParam(':part_id', $part_id, PDO::PARAM_INT); 
     } 
    $statement->bindParam(':user_id', $user_id, PDO::PARAM_INT); 
    $statement->bindParam(':solution', $description, PDO::PARAM_STR); 
    $statement->execute(); 
    $solution_id = $this->dbh->lastInsertId(); 
    } catch(PDOException $e) { 
     $this->dbh->rollBack(); 
     print_r($e->errorInfo); // Debug 
     return false; 
    } 
    $this->dbh->commit(); 
describe user_supplied_problem_solution; 

注(调试)print命令我把我留在了向您展示执行代码。这是构造时正确创建$this->dbh的对象的一部分。我通过命令行运行了这个查询,并且它成功了。这是我的输出:

mysql> INSERT INTO user_supplied_problem_solution (problem_id, user_id, solution) 
-> VALUES (130, 40, "Solution"); 
Query OK, 1 row affected (0.00 sec) 

这是我当我跑上面的方法签名createSolution("this is a test solution", 40)代码:

running the problem level query 
INSERT INTO user_supplied_problem_solution (problem_id, user_id, solution) 
       VALUES (:problem_id, :user_id, :solution) 
running the problem level binding 
The problem ID is 130 
Array 
(
    [0] => 42S22 
    [1] => 1054 
    [2] => Unknown column 'problem_id' in 'field list' 
) 

我多久,这是道歉,但我知道的第一反应像这样的问题是人们需要更多信息。

+0

确实选择该字段,使用PHP正常工作。您应该通过PHP运行您的描述查询并打印结果。是否有可能你有一个你连接到数据库的副本..等等,你必须验证PHP正在获得的结构。 – ArtisticPhoenix

+0

这是一个漫长的过程,但您使用PHP的用户是否拥有适当的权限。否则一切看起来很好。 – ArtisticPhoenix

+0

您确定在代码中访问与手动测试相同的数据库吗? – MaxZoom

回答

0

解决 - 这是由于代码以外的代码。我太狭隘了,看着一段错误的代码。运行此查询后,该方法将调用另一个具有该问题的私有方法。

获得的经验:不要快速复制和粘贴,如果你不能弄明白什么,请休息10分钟。

相关问题