2016-11-28 49 views
-1

(支持:Ubuntu的 - > phpstorm,Apache服务器,PDO)PHP PDO插入问题(阿贾克斯下拉提取)

大家好,

实际上,我想保存选择的值使用Ajax将下拉列表中的数据导入到我的PDO数据库中。

问题:我管理我的$ selectedOpt添加到我的数据库,但我尝试做相同的$ numLigne,在INSERT不工作了...... (我想要得到的值所选选项的行)的

这里是我的代码:

$file = fopen($fichier_txt, 'r+'); 
if ($file) 
{ 
    $pattern_GC = '/N°.*\d{4}(\s?\s?[\w\s]*)(\d{5})\s?(\w+)\W+/isU'; 
    $fichier_txt_content = file_get_contents($fichier_txt); 
    $regex_GC = preg_match_all($pattern_GC, $fichier_txt_content, $match_GC); 

    // Match regroupant nom/prenom + adresse 
    $match_un = explode(PHP_EOL, $match_GC[1][0]); 
    $match_nom_prenom = $match_un[2]; 
    $match_adresse = $match_un[3]; 
    // Match CP 
    $match_deux = $match_GC[2][0]; 
    // Match ville 
    $match_trois = $match_GC[3][0]; 
    // On place ces matchs dans un tableau pour faciliter la création des DDL 
    $opt = array(0 => $match_nom_prenom, 1 => $match_adresse,2 => $match_deux, 3 => $match_trois); 
    $numLigne = array(); 
    $numLigne = array_keys($opt); 
    $i = 0; 

    ?> 

    <!-- DEFINITION DES DROP-DOWN LISTS --> 
    <html> 
    <br /><br /> 
      <label>Nom :</label> 
      <form method="POST"> 
       <select name="selectBox" class="drop" id="Combobox1"> 
        <option selected hidden value="Fais ton choix !">Choisis le nom</option> 
       <?php foreach($opt as $key => $val) {?> 
        <option value="<?= $val ?>"><?= $val ?></option> 
       <?php } ?> 
       </select> 
      </form> 
    <br /><br /> 
    </html> 

    <html> 
     <label>Prenom :</label> 
     <form method="POST"> 
      <select name="selectBox" class="drop" id="Combobox2"> 
       <option selected hidden value="Fais ton choix !">Choisis le prenom</option> 
       <?php foreach($opt as $key => $val) {?> 
        <option value="<?= $val ?>"><?= $val ?></option> 
       <?php } ?> 
      </select> 
     </form> 
     <br /><br /> 
    </html> 
<?php } 

以非常相似的文件,我有我的Ajax调用

<script> 

// JS Script to save to DB + remove selected opt (jQuery + Ajax) 
$(document).ready(function saveToDatabase(selectedValue) 
{ 
    var select = selectedValue; 
    select = $(this).serialize(); 
    var selectBoxes = $('select'); 

    selectBoxes.on('focusin', function() 
    { 
     // Store the current value 
     $(this).data('value', this.value); 
    }); 

    selectBoxes.on('change', function() 
    { 
     // POST to php script 
     $.ajax 
     ({ 
      type: 'POST', 
      url: 'formulaire_2_test.php', 
      data:{selected:this.value} 
     }); 
     var oldValue = $(this).data('value'); 
     var newValue = this.value; 

     // Remove selected option for children selectBoxes 
     selectBoxes.filter(':not(#' + this.id + ')').each(function() 
     { 
      var options = $(this).children(); 
      options.filter('[value="' + oldValue + '"]').show(); 

      if (newValue !== '') 
      { 
       options.filter('[value="' + newValue + '"]').hide(); 
      } 
     }); 
    }); 
}); 

最后,我PDO连接语句:(位于另一个PHP文件)

<?php 

include 'formulaire_de_test.php'; 
// Connexion à la BDD pour save les lignes du form (PDO) 
try 
{ 
    $PDO = new PDO('mysql:host=localhost;dbname=autofill_data', 'root', 'password'); 
} 
catch (Exception $e) 
{ 
    die('Erreur : ' . $e->getMessage()); 
} 

// Récup de l'option selected et envoi vers la base 
$selectedOpt = $_POST['selected']; 
global $key; 
$req = $PDO->prepare("INSERT INTO data_lines(idDistributeur, numLigne, libelle) VALUES(0, :numLigne, :selectedOpt)"); 
$req->bindParam(':selectedOpt', $selectedOpt); 
$req->bindParam(':numLigne', $key); 
$req->execute(); 
$data = $req->fetchAll(); 

我的问题是坐落在我的 “numLigne” 变量,但我不明白在哪里精确...

一些帮助将不胜感激,因为我无法得到任何提前自上午9:00 ...

在此先感谢,

问候,

斯泰利奥KONTOS。

+0

你'numLigne'势必'$ key',但它不会是在您插入的文件中定义。你可以回显'$ key'来验证。如果你试图从你的表格中获得密钥,那么通过一些方法来传递它。你可以试试'',并且将下划线的值分解。 – aynber

+0

这就是我对我的$ numLigne的想法......我想要存储选定的选项,并将其行拖放到下拉列表中。做你刚写的内容可能授权我获得与我的价值相关的关键? –

+0

不管怎样,我都会试试这个,谢谢!你能在这里指导我吗?我到底需要在哪里添加爆炸声明来存储我的数据......? –

回答

1

正如我所提到的,如果您告诉它$ key将会通过。如果你把它添加到值,像这样:

<option value="<?= $key."_".$val ?>"><?= $val ?></option> 

你可以把它背出来是这样的:

$value = $_POST['selected']; 
$options = explode('_',$value); // Your key is in the first position (0), your option in the second (1) 

$req->bindParam(':selectedOpt', $options[1]); 
$req->bindParam(':numLigne', $options[0]); 
+0

好的,这几乎是我设法做的,我不确定bindParam寿。 我要马上测试一下。 –

+0

它工作,非常感谢,祝你有美好的一天! :) –