0

Following tutorials I found online and this one basically was showing me how to insert data into a database. I have Database called JapaneseDefinition and I have the table named Japanesewords. I got HTML Code that sends Japaneseword and English into my Database.

I am able to look up a word with a different query, but I am having no such look with the INSERT Query. Can someone look at my code and tell me where I am going wrong with it.

                    try{
$conn = new PDO('mysql:host=Jamal-PC;dbname=japanesewords',$username,$password);
if(isset($_POST['EnglishWord'])){
$Japaneseword = $_POST ['JapaneseWord'];
$Englishword = $_POST['EnglishWord'];
$q = "INSERT INTO Japanesedefinition(Japaneseword,Englishword)VALUES(:Japaneseword,:Englishword):";
$query = $conn->prepare($q);
$result = $query->execute(array(
'Japaneseword'=>$Japaneseword,
"Englishword"=>$Englishword
));
}
}catch(PDOException $e){
 echo 'ERROR: ' . $e->getMessage();
}
echo 'hello';
}
myTest();
?>
5
  • 1
    Can you post what errors you are getting? Commented Sep 12, 2013 at 22:51
  • 1
    You have a colon at the end of your query, not a semi-colon. Is this a typo on this post, or is it your problem? You also do not check if Japaneseword isset, but that probably isn't your issue. Commented Sep 12, 2013 at 22:51
  • That colon at the end needs to be removed as well Commented Sep 12, 2013 at 22:53
  • Are $username and $password are being set inside the myTest() function as well or just placeholders for the question here? Commented Sep 12, 2013 at 23:53
  • is an error being echoed? is error reporting turned on? Commented Sep 13, 2013 at 15:27

2 Answers 2

1

you need to add the colon's to your execute array keys.

$result = $query->execute(array(
':Japaneseword'=>$Japaneseword,
":Englishword"=>$Englishword
));

EDIT: if this is not solving the problem AND you removed the colon from the end of your query string (you have ):"; ), then the next step is to check your post data

//put this at the top of the script and make sure all your post 
//variables are set as expected
echo '<pre>';
print_r($_POST);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

7 Comments

@SmithSmithy It was not me who downvoted you, although your initial comment was worthy of it.
No worries. I did submit a one liner in error and then went back to paste in the revised code block.
I'm trying that and still not getting any data into my database.
@SmithSmithy I hardly if ever downvote anyone. I rather point it out first and hopefully add to an answer in order to provide accurate information for the OP/all. ;-)
at user1459917, bummer... see my revised answer. have you done this already?
|
1

You need to precede the token names in your array with a colon, like this:

$result = $query->execute(array(
 ':Japaneseword'=>$Japaneseword,
 ":Englishword"=>$Englishword
));

Comments

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.