0

here is my code the part of the option in php is not working somehow and it will only print team and question only when i tested

<?php
 Include("conifdata.php");

$command= "INSERT INTO SoccerPoll (Team, Question, Player1, Player2, Player3, Player4) VALUES (?,?,?,?,?,?)";
$stmt = $dbh ->prepare($command);
$stmt->execute(array($_POST['team'], $_POST['question'],$_POST['option[0]'], $_POST['option[1]'], $_POST['option[2]'], $_POST['option[3]']));
if($stmt ->rowCount()>0){
    echo "Successfull";
}

// when tested here it will only print team and question only i need all of them

 echo $_POST["team"], $_POST['question'], $_POST['option[0]'], $_POST['option[1]'], $_POST['option[2]'], $_POST['option[3]'];



 //and here is my html page

<h1>Create a Team Poll</h1>
<p>enter data.</p>
<form action="addPoll.php" method="POST">
Team: <input type="text" required name="team"><br>
Question: <input type="text" required name="question"><br>
Option 1: <input type="text" required name="option1"><br>
Option 2: <input type="text" required name="option2"><br>
Option 3: <input type="text" required name="option[]"><br>
Option 4: <input type="text" required name="option[]"><br>
<input type="submit" value="Create Poll">
</form>
4
  • replace the commas with periods. Commented Apr 6, 2015 at 22:47
  • "option[]" are all this but still cant fix and also i tried both single quote and double quote Commented Apr 6, 2015 at 22:50
  • 1
    @adeneo Why? echo can take multiple arguments and it echoes them all. Commented Apr 6, 2015 at 22:51
  • @Barmar - of course it does, confused with JS, and I should have seen those strange keys Commented Apr 6, 2015 at 22:56

2 Answers 2

2

When a parameter name ends with [...], that isn't part of the $_POST key, it causes that $_POST variable to be a sub-array. You need to access it with multi-dimensional array syntax. So $_POST['option[0]'] should be $_POST['option'][0], and similarly for all the other elements of that array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is what i was missing
0

your echo statement has commas, try using the period to concatenate them, or replace the , with ." ". if you want spaces between them

2 Comments

echo $foo, $bar is the same as echo $foo . $bar.
what is wrong with this code ` $command= "INSERT INTO SoccerPoll (Team, Question, Player1, Player2, Player3, Player4) VALUES (?,?,?,?,?,?)"; $stmt = $dbh ->prepare($command); $stmt->execute(array($_POST['team'], $_POST['question'], $_POST['option[0]'], $_POST['option[1]'], $_POST['option[2]'], $_POST['option[3]']));`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.