0

I am doing tag insert. So, I have several tags need to be insert into mysql. I explode it first, then I have a array[0],array[1],array[2]......., and I need to SELECT * FROM table WHERE key=array[0] and key=array[1] and key=array[2]......., instead of doing this way, I want to use i++, however, it doesn't work, I don't know why? Help please! appreciate!

<?php
       $tag=strtolower($_POST['tag']);
  $tag=explode(" ", $tag);
  include 'db_tag.php';

  for ($i = 0; $i <= 9; $i++){
  
  $stmt = $db->prepare ("SELECT key FROM keyword WHERE key = :tag"); 
   $tag_i=$tag[$i];
      
  $stmt->bindParam(':tag', $tag_i);
                                 }
     $stmt->execute();
  $row_tag = $stmt->fetchALL(PDO::FETCH_ASSOC);
  foreach ($row_tag as $row_tag){
   echo $row_tag['key'];
  }
?>
conan
  • 1,327
  • 1
  • 12
  • 27
  • Why do you make a `SELECT` when you have to `INSERT` the tags? If instead this is about _reading_ the tags from the database, then one `SELECT` should be enough. – arkascha Feb 13 '15 at 18:11
  • I didn't paste the whole code. sorry to confuse you. I need to do the select first, then do the if statement and insert. – conan Feb 13 '15 at 19:02
  • May I suggest that you review the questions you asked so far and see if there are any answers worth to be *accepted*, or at least give some feedback as to what's missing? If you keep asking without looking at answers properly, people will start downvoting/ignoring you. – didierc Feb 14 '15 at 01:57

2 Answers2

1

For a SELECT :

First construct your SQL code:

$stmt = 'SELECT columns FROM keyword WHERE key = :tag0');
for ($i = 1; $i <= 9; $i++){
  $stmt .= ' OR key = :tag' . $i;
}

Then fill up the params:

$stmt = $db->prepare($stmt);
for ($i = 0; $i <= 9; $i++){        
  $stmt->bindParam(':tag'+$i, $tag[$i]);
}

By the way, in your code you are trying to select a column (key) which is fixed in the WHERE predicates, it's probably not what you want, since you will get exactly the data you've put in the query parameters to begin with.

For an INSERT:

See this question.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52
  • 1
    Thanks for you response. I had tried your code. But it didn't work. I also tried to change $tag[i] to $tag[$i], but it's not working. – conan Feb 13 '15 at 19:04
  • Thank you for pointing it out. I think I wrote that code a bit hastily without double checking. It should be correct now (up to the column names in the query). – didierc Feb 13 '15 at 19:15
  • 1
    Thank you again. But you need to change the + to . for append. Then, it works. Appreciate your help. – conan Feb 13 '15 at 19:43
  • check @AmhedDaou 's answer, it's better. – didierc Feb 13 '15 at 19:50
1

I suggest IN STATEMENT (The IN operator allows you to specify multiple values in a WHERE clause.)

$in = join(',', array_fill(0, count($tag), '?'));
$stmt = $db->prepare ("SELECT key FROM keyword WHERE key IN ($in)"); 
$stmt->execute($tag);

The catch is to put as many question marks as parameters so use my code if it is dynamic otherwise you can hardcode it.

meda
  • 45,103
  • 14
  • 92
  • 122