0

I have a array stored in $_POST['extras'] and when I implode it to a string which the result could be something like "1,4" it makes bugging my Mysql prepared query

For instance this doesn't works (only first row returned)

if(isset($_POST['extras'])){
$string_extras = implode(",", $_POST['extras']);

var_dump($_POST['extras']);
//display : array(2) { [0]=> string(1) "1" [1]=> string(1) "4" } in my case !

var_dump($string_extras);
//display :  string(3) "1,4" in my case !

$req7 = $DB->query('SELECT * FROM `Option_extra_PT` WHERE `id_option_extra` IN(:post_extras)');
$req7->bindParam(':post_extras', $string_extras);
$req7->execute();
// While loop
while ($data_req7 = $req7->fetch(PDO::FETCH_ASSOC)) {

//Display result of Query
echo ' <div class="extra_Bit">
                                                        <div class="extra_libelle"><label>(ref. '.$data_req7['id_option_extra'].')'.$data_req7['nom_option_extra'].'</label></div>
                                                        <div class="extra_prix">'.$data_req7['prix_option_extra'].' €</div>
                                                        <div class="clearfix"></div>
                                                    </div>';
//for me only the first row is returned !                                                    
              }
              
              }

And this works properly (but don't suit me because the result of $_POST won't be always the same of course... and can't be replace by the value 1,4) :

$req7 = $DB->query('SELECT * FROM `Option_extra_PT` WHERE `id_option_extra` IN(1,4)');
// While loop
while ($data_req7 = $req7->fetch(PDO::FETCH_ASSOC)) {

//Display result of Query
echo ' <div class="extra_Bit">
                                                        <div class="extra_libelle"><label>(ref. '.$data_req7['id_option_extra'].')'.$data_req7['nom_option_extra'].'</label></div>
                                                        <div class="extra_prix">'.$data_req7['prix_option_extra'].' €</div>
                                                        <div class="clearfix"></div>
                                                    </div>';
//for me this works perfectly !                                                    

}
              
Shadow
  • 33,525
  • 10
  • 51
  • 64
B.BDC
  • 1
  • 1
  • When you bind a value in PDO it needs to be a singular value, not an array. For something more sophisticated try something like [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/5.4/eloquent). – tadman Mar 09 '17 at 02:30
  • this should help http://stackoverflow.com/questions/14767530/php-using-pdo-with-in-clause-array – Yaman Jain Mar 09 '17 at 02:32
  • var_dump($string_extras); //display : string(3) "1,4" in my case ! >> And $req7->bindParam(':post_extras', $string_extras); >> So Bind is not an array...What is wrong ? – B.BDC Mar 09 '17 at 09:20
  • Thank you Yaman Jain, I use now this suggest : http://stackoverflow.com/a/19148390/7666388 with FIND_IN_SET(`id_option_extra`, :post_extras) in my prepared query and it's works Amazing ! – B.BDC Mar 09 '17 at 09:37
  • When using user submitted values, use **PREPARE()**. Someling like `str_repeat(',?', count($string_extras))` when building that prepare `$sql`. – Xorifelse Mar 09 '17 at 19:32

0 Answers0