2016-04-24 10 views
0

Das ist also mein Code setzen:Wie PHP-Variablen in einer SQL-Abfrage

<nav class="navbar navbar-inverse"> 
    <div class="container-fluid"> 
     <div class="navbar-header"> 
      <a class="navbar-brand" href="#">WebSiteName</a> 
     </div> 

     <ul class="nav navbar-nav"> 
      <li class="active"><a href="#">Home</a></li> 

      <?php 
       $dbconn= new PDO('sqlite:negozio.db'); 


      $sqlcate = "SELECT * FROM categoria"; 
      foreach($dbconn->query($sqlcate) as $row) { ?> 
      <li class="dropdown"> 
      <a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $row['des_categoria']; ?> <span class="caret"></span></a> 

      <ul class="dropdown-menu"> 
       <?php 
       $sqltipocate = "SELECT tipo.des_tipo FROM tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria AND tipo_cate.id_tipo = tipo.id_tipo AND tipo_cate.id_categoria = " . $row['id_categoria'] . " "" "; 

       foreach($dbconn->query($sqltipocate) as $row1) { ?> 

       <li><a href="#"><?php echo $row1['des_tipo']; ?></a></li> 
      <?php } ?> 
      </ul> 
     </li> 
     <?php } ?> 

     </ul> 
    </div> 
</nav> 

Alles funktioniert gut, bis $sqltipocate blabla ...

Dieser Fehler erscheint immer:

Syntaxfehler, unerwartet T_CONSTANT_ENCAPSED_STRING

Kann jemand Hilf mir?

+0

@Anant, warum Sie dies als eine Antwort nicht hinzufügen? – webeno

+0

@Antant aber andere können es sehen und es könnte ihnen helfen, also könnten sie upvote und du würdest mehr Ansehen bekommen, oder ich denke, das ist nicht mehr viel Motivation bei 10K rep? – webeno

+0

@Anant gut, anscheinend ist es das, was ich getan habe (ich merke nicht, dass es schon da war als Kommentar von dir) und tatsächlich habe ich nichts als downvotes, also habe ich es gelöscht ... nun, lass mich weitergehen ... – webeno

Antwort

0

Es ist Verkettungsproblem wie in früheren Antworten betrachtet. Ich kann etwas hinzufügen, das dein Leben erleichtern kann. Es ist sprintf

Ihr SQL-Abfrage-String wird die format Parameter sprintf wie folgt:

  $sqltipocate = sprintf("SELECT tipo.des_tipo FROM tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria AND tipo_cate.id_tipo = tipo.id_tipo AND tipo_cate.id_categoria = %d", $row['id_categoria']); 
0

Verwenden geschweiften Klammern.

Beispiel:

$id = 1; 
$sql = "SELECT * FROM something WHERE id = {$id}"; 

echo $sql; 
//SELECT * FROM something WHERE id = 1 

Link zu Code: http://codepad.org/V9QasGHH

0

Speichern Sie Ihre $ row [ 'id_categoria'] in Variable wie

$ id = $ row [ 'id_categoria'];

Dann legen Sie $ id in Abfrage wie:

$ sqltipocate = „SELECT tipo.des_tipo VON tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria UND tipo_cate.id_tipo = tipo.id_tipo UND tipo_cate.id_categoria = '". $ id."' ";

dieses tipo_cate.id_categoria Siehe = ' "$ id"'