2014-03-04 198 views
0

我有這個準備好的聲明,我不知道如何來遍歷,它看起來像這樣:PHP預處理語句foreach循環

<?php 
    //if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
     require('../includes/db_connect.php'); 
     $i = 40; 
       foreach ($_POST['item'] as $value) { 

      /* Register a prepared statement */ 
      if ($stmt = $mysqli->prepare(' 

      UPDATE house_room1 SET z = ? WHERE object_id = ? 

      ')) { 
       /* Bind parametres */ 
       $stmt->bind_param('ii', $object_id, $i); 

       /* Insert the parameter values */ 
       $object_id = 1; 
       $i = $i; 

       /* Execute the query */ 
       $stmt->execute(); 

       /* Close statement */ 
       $stmt->close(); 

      } else { 
       /* Something went wrong */ 
       echo 'Something went terribly wrong' . $mysqli->error; 
      } 
      $i++; 
     } 
    //} 
?> 

我希望它在z添加一個新的價值,但現在它甚至不知道$i是什麼。呃,任何想法,建議或建議?提前致謝。

+0

'$ I = $ I;'你在哪裏最初設置'$ i'的價值? –

+0

$ _POST ['item']在哪裏起作用?這是否成爲object_id? – larsAnders

回答

0

改變這一點:

require('../includes/db_connect.php'); 

/* Insert the parameter values */ 
$object_id = 1; 
$i = 40; 

foreach ($_POST['item'] as $value) { 

    /* Register a prepared statement */ 
    if ($stmt = $mysqli->prepare('UPDATE house_room1 SET z = ? WHERE object_id = ?')) { 
     /* Bind parametres */ 
     $stmt->bind_param('ii', $i, $object_id); 

     /* Execute the query */ 
     $stmt->execute(); 

     /* Close statement */ 
     $stmt->close(); 

    } else { 
     /* Something went wrong */ 
     echo 'Something went terribly wrong' . $mysqli->error; 
    } 
    $i++; 
} 
+0

爲什麼這會降低投票率?謝謝btw – owwyess

+0

@owwyess:我不知道。那不是我! :S – JellyBelly