SQLSTATE [42S22]:未找到列:1054 Champ'nom_complet'anconnu dans字段列表

时间:2014-09-19 13:35:12

标签: php mysql pdo

我有一张桌子“候选人”。当我想显示所选项目时,我收到了此消息

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054   Champ 'nom_complet' inconnu dans field list' in C:\wamp\www\CVtheque\Model\Connexion.php on line 78

PDOException: SQLSTATE[42S22]: Column not found: 1054 Champ 'nom_complet' inconnu dans field list in C:\wamp\www\CVtheque\Model\Connexion.php on line 78

表候选人:

CREATE TABLE candidat (
  id_candidat  int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
  civ  varchar(14) NOT NULL,
  nom_complet  varchar(150) NOT NULL,
  email  varchar(300) NOT NULL,
  pass  varchar(250) NOT NULL,
  tel varchar(14) NOT NULL,
  adresse varchar(500) NOT NULL,
  pays varchar(150) NOT NULL,
  ville varchar(150) NOT NULL,
  cv varchar(1000) NOT NULL,
  niv varchar(40) NOT NULL,
  fonction varchar(100) NOT NULL,
  profil varchar(100) NOT NULL,
  nbre_exp varchar(50) NOT NULL,
  dispo varchar(100) NOT NULL,
  tech varchar(300) NOT NULL )

Connexion.php:     

 Class Connexion {
  private $host = "localhost";
  private $user = "root";
  private $pass = "";
  private $dbname = "cvtheque";
  private $dbh;
  private $stmt;

    public function __construct() {
      $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
      $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
        );
        try {
          $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        catch (PDOException $e) {
          echo "Erreur de la connexion: " . $e->getMessage();
     }
   }
    public function get_connexion() {
      $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
      $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
          $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
     }
        catch (PDOException $e) {
          echo "Connexion Error: " . $e->getMessage();
     }
     return $this->dbh;
 }

    public function query($query) {
      $this->stmt = $this->dbh->prepare($query);
}

    public function bind($param, $value, $type = null) {
      if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
      }
    $this->stmt->bindValue($param, $value, $type);
    }

    public function execute() {
      return $this->stmt->execute();
    }

    // returns an array of the result set rows
    public function resultset() {
      $this->execute();
      return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    //returns a single record from the database
    public function single() {
      $this->execute();
      return $this->stmt->fetch(PDO::FETCH_ASSOC);
     }

    //returns the nb of effected rows from the previous delete, update or insert statement
    public function rowCount() {
      return $this->stmt->rowCount();
     }

    public function lastInsertId() {
      return $this->dbh->lastInsertId();
     }

    public function beginTransaction() {
      return $this->dbh->beginTransaction();
     }

    //end a transaction and commit your changes:
    public function endTransaction() {
      return $this->dbh->commit();
     }
    //cancel a transaction and roll back your changes:
    public function cancelTransaction() {
      return $this->dbh->rollBack();
     }

    public function debugDumpParams() {
      return $this->stmt->debugDumpParams();
     }
} 

的search.php:

<html>
  <head>
        <title>Test de la recherche</title>
        <meta charset="utf-8">
  </head>
  <body>
        <form method="POST" action="Datatable.php">
            Fonction: <select name="fonc">
                        <option value="0">Tous</option>
                        <option value="1"> Agriculture / Pêche / Navigation  </option>
                        <option value="2"> Architecture / Urbanisme / Environnement  </option>
                        <option value="3"> Artisanat  </option>
                        <option value="4"> Banque / Assurance  </option>
                        <option value="5"> BTP / Extraction  </option>
                        <option value="6"> Commerce / Distribution / Vente / Import-export  </option>
                        <option value="7"> Communication / Publicité  </option>
                        <option value="8"> Comptabilité / Gestion / Audit  </option>
                        <option value="9"> Conseil / Consulting  </option>
                        <option value="10"> Direction / Gérance  </option>
                        <option value="11"> Electricité / Electronique / Electrotechnique  </option>
                        <option value="12"> Achats  </option>
                        <option value="13"> Enseignement / Formation / Traduction  </option>
                        <option value="14"> Immobilier  </option>
                        <option value="15"> Informatique / Internet / Multimedia  </option>
                        <option value="16"> Journalisme / Audiovisuel / Médias  </option>
                        <option value="17"> Juridique / Droit  </option>
                        <option value="18"> Logistique / Transport  </option>
                        <option value="19"> Marketing  </option>
                        <option value="20"> Métiers des centres d'appels  </option>
                        <option value="21"> Production / Maintenance  </option>
                        <option value="22"> Qualité / Sécurité / Hygiène / Environnement  </option>
                        <option value="23"> R &amp; D  </option>
                        <option value="24"> RH / Recrutement / Formation  </option>
                        <option value="25"> Santé / Professions médicales  </option>
                        <option value="26"> Secrétariat / Services Généraux  </option>
                        <option value="27"> Télécoms / Réseaux  </option>
            </select><br/>
            Nombre d'experience: <input type="text" name="nex" ><br/>
            Disponibilité: <input type="text"name="dispo"><br/>
            Téchnologies: <textarea name="tech" rows="4" cols="50" class="textfield"></textarea><br/>
            <input type="submit" value="valider" />
        </form>
  </body>
</html>

这是应该在datatble中显示结果的页面

<?php
require_once '../Model/Connexion.php';
$fonc = $_POST['fonc'];
$nex = $_POST['nex'];
$dispo = $_POST['dispo'];
$tech = $_POST['tech'];
$c = new Connexion();
$c->query("select nom_complet,fonction,email,nbre_exp,dispo,tech from candidat "
        . "where fonction = :fonc and nbre_exp = :nexp and dispo = :dispo and tech = :tech");
$c->bind(':fonc', '$fonc');//pour éviter l'injection SQL
$c->bind(':nexp', '$nex');//pour éviter l'injection SQL
$c->bind(':dispo', '$dispo');//pour éviter l'injection SQL
$c->bind(':tech', '$tech');//pour éviter l'injection SQL
$rows = $c->resultset();
?>
<html>
    <head>
                <title>Search results</title>
<   /head>
<?php
        $c->query("SELECT nom_complet, fonction, email, nbre_exp,dispo,tech "
                . "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="."'candidat'");
        $cl = $c->single();
    ?>
    <body>
        <table cellpadding="0" cellspacing="0" border="0">
                <thead>
                        <tr>
                            <th><?php echo($cl['nom_complet']); ?></th>
                            <th><?php echo($cl['fonction']); ?></th>
                            <th><?php echo($cl['email']); ?></th>
                            <th><?php echo($cl['nbre_exp']); ?></th>
                            <th><?php echo($cl['dispo']); ?></th>
                            <th><?php echo($cl['tech']); ?></th>
                        </tr>
                </thead>
                <tfoot>
                        <tr>
                            <th><?php echo($cl['nom_complet']); ?></th>
                            <th><?php echo($cl['fonction']); ?></th>
                            <th><?php echo ($cl['email']); ?></th>
                            <th><?php echo($cl['nbre_exp']); ?></th>
                            <th><?php echo($cl['dispo']); ?></th>
                            <th><?php echo($cl['tech']); ?></th>
                        </tr>
                </tfoot>
                <tbody>
                    <?php foreach($rows as $r) {  ?>
                        <tr class="odd_gradeX">
                                <td class="read_only"><?php echo($r['nom_complet']) ?></td>
                                <td><?php echo($r['fonction']) ?></td>
                                <td><?php echo($r['email']) ?></td>
                                <td><?php echo($r['nbre_exp']) ?></td>
                                <td><?php echo($r['dispo']) ?></td>
                                <td><?php echo($r['tech']) ?></td>
                        </tr>
                    <?php } ?>            
                </tbody>
        </table>

    </body>
</html>

我想在表单中进行搜索并将结果显示在另一页中。

2 个答案:

答案 0 :(得分:1)

你这样做:

$c->query("SELECT nom_complet, fonction, email, nbre_exp,dispo,tech "
        . "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="."'candidat'");

我的猜测是表INFORMATION_SCHEMA.COLUMNS不包含名为nom_complet的列。

答案 1 :(得分:1)

请参阅有关INFORMATION_SCHEMA COLUMNS Table

的MySQL文档

尝试使用这样的SQL: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='candidat' AND COLUMN_NAME IN ('nom_complet', 'fonction', 'email', 'nbre_exp', 'dispo', 'tech');

然后使用$cl = $c->resultset();

然后使用foreach从您需要的$cl获取所有数据,并将其显示在您的网页上。