Rolf B: in einer foreach Schleife $_POST hört nach einem Durchlauf auf?

Beitrag lesen

Hallo Michi,

die Suchschleife ist unnötig. Du kannst einfach abfragen, ob $row['id'] in $_POST enthalten ist und dann darauf zugreifen.

foreach($_POST AS $question => $answer) 
  {
  if($question==$row['id'])
    {
    #$statement = $pdo->prepare("INSERT INTO table2 SET id_user =:id_user, question =:question");
    #$result = $statement->execute(array('id_user' => $_SESSION['userid'], 'question' => $question ));		
    echo $question;
    echo'<br/>';	
    }
  }

Die Suchschleife ist unnötig.

$questionStmt = $pdo->prepare("select a FROM table  WHERE port =:port");
$insertStmt = $pdo->prepare("INSERT INTO table2 SET id_user =:id_user, question =:question");
// ...
while($row = $questionStmt->fetch())
  {
  $question = $row['id'];
  if (isset($_POST[$question])
    {
    $answer = $_POST[$question];
    $result = $insertStmt->execute(array('id_user' => $_SESSION['userid'], 'question' => $question ));		
    echo $question;
    echo'<br/>';	
    }
  }

Oder noch kürzer (die $answer Zeile hab ich stehen lassen, falls Du die noch anderswo brauchst)

$questionStmt = $pdo->prepare("select a FROM table  WHERE port =:port");
$insertStmt = $pdo->prepare("INSERT INTO table2 SET id_user =:id_user, question =:question");
// ...
while($row = $questionStmt->fetch())
  {
  if (isset($_POST[$row['id']])
    {
    $answer = $_POST[$row['id']];
    $result = $insertStmt->execute(array('id_user' => $_SESSION['userid'], 'question' => $row['id'));		
    echo $row['id'] . '<br/>';	
    }
  }

Rolf

--
sumpsi - posui - clusi