在sql字符串中放置列表中的变量

时间:2014-04-18 11:26:41

标签: php mysql

我创建了一个名为myauthors的mySQL数据库,在myauthors中我创建了一个名为table 1的表。然后我创建了4个选择框(请参阅代码)。之后我用while循环创建了一些代码和一个sql查询来填写输出(打印或回声)。工作正常并将表格内容打印到屏幕上。但这就是我想要做的。当用户点击选择框中的一个值时,我希望将它放在一个变量中,然后将其放入sql查询行。我尝试将其添加到

下面的代码
    $aa = "author" 
    $bb = "publisher"
    $cc = "year"
    $dd = "genre"

" "是选择框的名称。 这对于查询

'"$aa"' AND publisher= '"$bb"' AND year = '"$cc"' AND genre ='"$dd"' " "; 

但它没有用。 我是在吠叫错误的树,还有另一种做法。 非常感谢吉姆

  <!DOCTYPE HTML>
  <html>
  <body>


  <select name = "author"> 
  <option value="kendavies">ken davies</option>
  <option value="arthursmith">arthur smith</option>
  <option value="gillrafferty">gill rafferty</option>
  <option value="mollybrown">molly brown</option>
  <option value="gilbert riley">gilbert riley</option>
  <option value="colinwilson">colin wilson</option>
  <option value="jamesgreen">james green</option>
   <option value="arnoldlaing">arnold laing</option>
  <option value="cathyellis">cathy ellis</option>
  <option value="carolreed">carol reed</option>    

  </select>

 <select name = "publisher"> 
 <option value="yonkers">yonkers</option>
  <option value="blueparrot">blue parrot</option>
  <option value="zoot">zoot</option>   

  </select>
    <select name = "year"> 
  <option value="2003">2003</option>
  <option value="2004">2004</option>
  <option value="2005">2005</option>
  <option value="2006">2006</option>
  <option value="2007">2007</option>
  <option value="2008">2008</option>

  </select>

   <select name = "genre"> 
   <option value="adventure">adventure</option>
   <option value="thriller">thriller</option>
  <option value="crime">crime</option>
  <option value="biography">biography</option>
   <option value="romance">romance</option>     

  </select>

      </body>
 </html>       
<?php             
 mysql_connect ("localhost","root","") or die(mysql_error());
 mysql_select_db ("myauthors") or die(mysql_error());  
 $strSQL = "SELECT * FROM `table 1`"; 
 $rs = mysql_query($strSQL);    



while($row = mysql_fetch_array($rs) ) {  

print $row ['ID']."<br/>";             
print $row ['author']."<br/>";   
print $row ['booktitle']."<br/>";
print $row ['publisher']."<br/>";
print $row ['yearpublished']."<br/>";
 print $row ['genre']."<br/>";
print $row ['copiessold']."<br/>";

 }
   mysql_close();
    ?> 
  </body>  
  </html>       

我已经将变量和新的sql行添加到代码中,但它现在仍然无法正常运行代码。

 <!DOCTYPE HTML>
  <html>
  <body>


  <select name = "author"> 
  <option value="kendavies">ken davies</option>
   <option value="arthursmith">arthur smith</option>
  <option value="gillrafferty">gill rafferty</option>
  <option value="mollybrown">molly brown</option>
  <option value="gilbert riley">gilbert riley</option>
   <option value="colinwilson">colin wilson</option>
    <option value="jamesgreen">james green</option>
   <option value="arnoldlaing">arnold laing</option>
  <option value="cathyellis">cathy ellis</option>
   <option value="carolreed">carol reed</option>    

  </select>

  <select name = "publisher"> 
  <option value="yonkers">yonkers</option>
   <option value="blueparrot">blue parrot</option>
  <option value="zoot">zoot</option>   

  </select>
    <select name = "year"> 
  <option value="2003">2003</option>
   <option value="2004">2004</option>
  <option value="2005">2005</option>
  <option value="2006">2006</option>
  <option value="2007">2007</option>
  <option value="2008">2008</option>

  </select>

   <select name = "genre"> 
  <option value="adventure">adventure</option>
   <option value="thriller">thriller</option>
  <option value="crime">crime</option>
  <option value="biography">biography</option>
  <option value="romance">romance</option>     

  </select>
  $aa = "author" 
  $bb = "publisher"
  $cc = "year"
  $dd = "genre" 
      </body>  
</html>       
<?php    

  $aa = "author"; 
  $bb = "publisher";
  $cc = "year";
  $dd = "genre";     
 mysql_connect ("localhost","root","") or die(mysql_error());
 mysql_select_db ("myauthors") or die(mysql_error());  
  $strSQL = "SELECT * FROM `table 1`WHERE author = '$aa' AND publisher = '$bb'       AND  year = '$cc' AND genre ='$dd'  ";  
  $rs = mysql_query($strSQL);    



  while($row = mysql_fetch_array($rs) ) {  

 print $row ['ID']."<br/>";             
 print $row ['author']."<br/>";   
  print $row ['booktitle']."<br/>";
 print $row ['publisher']."<br/>";
 print $row ['yearpublished']."<br/>";
 print $row ['genre']."<br/>";
 print $row ['copiessold']."<br/>";


  }
 mysql_close();
 ?> 
 </body>  
 </html>       

2 个答案:

答案 0 :(得分:0)

试试这个

"'$aa' AND publisher= '$bb' AND year = '$cc' AND genre = '$dd'"; 

答案 1 :(得分:0)

Concat with dots .

'".$aa."' AND publisher= '".$bb."' AND year = '".$cc."' AND genre ='".$dd."' ";