2015-05-04 123 views
0

我遇到了一个小问题,现在已经让我烦恼几个小时了。 所以,我有一个下拉,这是由于SQL请求填充,现在我想让选定的答案做另一个SQL请求,但我遇到麻烦... 这是我的代码。SQL请求取决于下拉菜单

<?php 
    try 
    { 
     $bdd = new PDO('mysql:host=localhost;dbname=PSF;charset=utf8','user','pass'); 
    } 
    catch (Exception $e) 
    { 
     die('Erreur : ' . $e->getMessage()); 
    } 

    echo 'poil'; 
    $choiceMicroscope = $bdd->query('select table_name from information_schema.tables where table_schema="PSF"'); 

?> 
    <form method="POST" action="test3.php"> 
    <select name="Microscope"> 


    <?php 

    while($choice = $choiceMicroscope->fetch()) 
    { 
     ?> 
     <option value="<?php echo $choice['table_name']; ?>"><?php echo $choice['table_name']; ?></option> 
     <?php 
    } 
    ?> 
    </select> 
    <input type="Submit" value="Send" name="Send"> 
    </form> 

    <?php 

    if(isset($_POST['Microscope']) and isset($_POST['Send'])) 
     $nom=$_POST['Microscope']; 
    else 
     $nom=""; 

    //On vérifie si les champs sont vides 
    if(empty($nom)) 
     echo '<font color = "red">The Microscope field should not be empty !</font>'; 
    else 
    { 
     echo $nom; 
     echo "là"; 
     $test = $bdd->query('Select * from ".$nom." where ObjMagn="40x" and NumberBead = 2;'); 
     echo "prout"; 
     echo gettype($test); 
     $test2 = $test->fetchAll(); 
     echo "ici"; 
     print_r($test2); 
     echo $_POST['Microscope']; 
    } 
    ?> 

当我测试了我的请求,我得到

AxioObserverlàbool(false) proutboolean 

这意味着,我的要求是不工作...

任何想法?谢谢 !

回答

0

您的查询与预期不符。您正在使用单引号,因此该变量未被连接。

$test = $bdd->query('Select * from ' . $nom . ' where ObjMagn="40x" and NumberBead = 2;'); 

这也可能会失败。

  1. 为什么给用户选择表格的能力?
  2. 你也可以使用这个SQL注入。
  3. 如果该值为空,则默认值为空,这将导致查询失败。
+0

好,所以这个请求的作品!非常感谢你 !我确信我已经尝试过... 1.其实这是根据选择建立图表。 2.数据库位于NAS上,只有与我合作的人才能使用它。 3.是的,我知道,但我仍然在那部分工作.. –

+0

啊,好吧,那么我猜得不错。如果向公众发布,请小心。如果您确定已解决问题,请将其标记为已接受的答案。谢谢 – chris85

+0

是的,这个下拉列表是从哪个表中选择我需要选择的行。我还有其他的事情要看我的网页是完整的。 表名实际上是多个显微镜名称(或我在此工作的地方使用的名称)。 –