0

This is my code :

It gives no error when i change the array to index type instead of associative. But the moment i change it back to associative it starts giving error. Any help on this ?

$dbh=new PDO("mysql:host=localhost;dbname=sj_db", 'root', '');

$entryData = array(
    "Team Name"=>$_POST['name']
  , "Won"=>$_POST['w']
  , "Lost"=>$_POST['l']
  , "Draw"=>$_POST['d']
  , "Points"=>$_POST['p']
  );


$sql="INSERT INTO fb (`Team Name`, Won, Lost, Draw, Points) VALUES (?, ?, ?, ?, ?)";
$sth=$dbh->prepare($sql);
//$sth->execute($entryData[`Team Name`],$entryData['Won'],$entryData['Lost'],$entryData['Draw']
//  ,$entryData['Points']);
$sth->execute($entryData);

//$sth->closeCursor();
3
  • Your $entryData associative array has 10 items in it, five values and five keys. You need to have just the values in there. Commented Sep 6, 2013 at 13:49
  • But i needed the keys for further uses. Any way to do that ? Commented Sep 6, 2013 at 13:50
  • 1
    -1 for just stating "it starts giving error" but not posting the error itself. Commented Sep 6, 2013 at 13:56

1 Answer 1

1

Placeholders in your query are positional (?) ones.
Either change them to named (:name)
or pass array_values($entryData) into execute

Though you have to remove a space from Team Name key in order to use named placeholders

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

7 Comments

And about the space thing.. Can't i use ` instead of ' for Team Name...wudnt that do ?
Neither ` nor ' symbol has nothing to do with placeholders, mind you. That's why you've been told to remove a space - that's the only way
Then it means there's no way to use placeholders for the columns having a space in their name ?
Add to your question what have you tried. We cannot know it just from your words "i did try"
Who said it's impossible? remove a space from the key name as you have been told
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.