snookian
snookian

Reputation: 873

While loop and PHP/MYSQL insert statement

Am I able or how can I create a while loop to run through some insert statemtns. I have some code below that I have tried to use using i++ but I keep getting errors in relation to where I have put $i such as:

Object of class PDOStatement could not be converted to string

Its hard to explain the previous code that amounts to this insert setup, but basically I decalre some variables and then they get bind and inserted with the following code but I have like 30 thins that could be inserted hence the while loop. So the outcome should be supplier_name2 - supplier_name30 and same for address etc etc.

   while ($i <= 30):
      //LINE TWO
      $suppliername = $this->supplier_name.$i;
      if(!empty($suppliername)){
          $sql0.$i = "INSERT INTO suppliers (supplier_name, supplier_address) 
                          VALUES (:supplier_name.$i, :supplier_address.$i) ";   
          $st0.$i = $conn->prepare ( $sql0.$i );
          $st0.$i->bindValue( ":supplier_name".$i, $this->supplier_name.$i, PDO::PARAM_STR );
          $st0.$i->bindValue( ":supplier_address".$i, $this->supplier_address.$i, PDO::PARAM_STR);
          $st0.$i->execute();
          $this->id = $conn->lastInsertId();
          $supplierid.$i = $this->id = $conn->lastInsertId();
        }  
    $i++; 
    endwhile;

Any help appreciated. Ian

Upvotes: 1

Views: 1034

Answers (3)

Krish R
Krish R

Reputation: 22711

Can you try this, below one insert supplier_name1 - supplier_name30 in tables.

          while ($i <= 30):
              //LINE TWO
              $suppliername = $this->supplier_name.$i;
              $supplier_address = $this->supplier_address.$i;
              if(!empty($suppliername)){
                $sql0 = "INSERT INTO suppliers (supplier_name, supplier_address)
                VALUES (:supplier_name, :supplier_address) ";
                $st0 = $conn->prepare ( $sql0 );
                $st0->bindValue( ":supplier_name", $suppliername, PDO::PARAM_STR );
                $st0->bindValue( ":supplier_address", $supplier_address, PDO::PARAM_STR);
                        $st0.$i->execute();
                        $this->id = $conn->lastInsertId();
                $supplierid = $this->id = $conn->lastInsertId();
              }
              $i++;
          endwhile;

Upvotes: 0

TabLand
TabLand

Reputation: 26

In > "supplier_name.$i" the dot is literal. So it becomes "supplier_name.1", "supplier_name.2" etc

In ":supplier_address".$i the dot is not literal, it is used as the concatenation operator. Hence you get "supplier_name1" and "supplier_name2" etc. Hence no replacement occurs I assume.

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

Expression $sql0.$i will be interpreted as string so PHP will try to use your PDOStatement as string. It's not the way you should use prepared statements.

Move your prepared statement before loop with some modifications.

$sql = "INSERT INTO suppliers (supplier_name, supplier_address) 
                      VALUES (:supplier_name, :supplier_address) ";   
$statement = $conn->prepare ( $sql );

Then perform bindValue() and execute() on $statement variable. Idea is to declared once and use few times. Also don't use $i in attributes names, it's unnecessary.

Upvotes: 1

Related Questions