1

my query is not inserting and i'm not getting any errors. can't figure out why it's not inserting

foreach($_POST as $key => $value) {
  $clean[$key] = mysql_real_escape_string($value);
}

if(isset($_POST['submit'])) {
$entry = "INSERT INTO test (Word, Type, Lang, Country, Gender, Advice, y_Advice, Notes, 
EditorNotify, Equiv) 
VALUES('".$clean["word_field"]."', 
'".$clean["type_field"]."', 
'".$clean["lang"]."', 
'".$clean["Country"]."', 
'".$clean["gender"]."', 
'".$clean["advice"]."', 
'".$clean["y_advice"]."', 
'".$clean["Notes"]."',
'".($clean["Notes"] != '' ? '1' : '')."',
'".$clean["Equiv"]."')";
echo mysql_query ($entry);
mysql_query ($entry);
5
  • 3
    Do a echo mysql_error(); to see errors Commented Nov 9, 2010 at 11:59
  • What's the output from echo mysql_error(); after your query executes? us3.php.net/manual/en/function.mysql-error.php Commented Nov 9, 2010 at 12:00
  • maybe you dont enter to condition because $_POST['submit'] is not set Commented Nov 9, 2010 at 12:00
  • @Haim you're right, it's 10 to 10. I was too quick :) Commented Nov 9, 2010 at 12:02
  • @Pekka no errors. can I use the for name something like set a form name to add_entry then do something like if(isset($_POST['add_entry'])) Commented Nov 9, 2010 at 12:29

3 Answers 3

1

You're actually doing the insert twice because of this:

echo mysql_query ($entry);
mysql_query ($entry);

The echo line will run the query, and so will the line after it. You need to get rid of that. (though I guess you only put it in there for testing purposes?)

Instead of that, I'd suggest just echoing $entry itself, so you get to see the finished SQL string. You may spot something wrong with the query right away just from that.

If you don't, then try copying+pasting that string into a SQL query program to see what the actual error is. That'll allow you to play with the query until you get it right.

You could also use the PHP command mysql_error() to get the error out of PHP, but it's when you've got a weird SQL error, it can often be quicker and easier to play with the query directly rather than within the PHP code.

hope that helps.

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

1 Comment

thanks that helped me, echoing $entry I noticed that two fields where not correct.
1

Try replacing:

(Word, Type, Lang, Country, Gender, Advice, y_Advice, Notes, 
EditorNotify, Equiv) 

With:

(`Word`, `Type`, `Lang`, `Country`,`Gender`, `Advice`, `y_Advice`, `Notes`, 
`EditorNotify`, `Equiv`) 

2 Comments

None of the column names are in the MySQL list of reserved words, so this shouldn't make any difference
@Mark though it's always worth to do it
1

You don't know whether you're getting any errors. First, get rid of echo mysql_query(). Then run:

mysql_query($query) or die(mysql_error());

If mysql_query() returns false, which it does upon failure, whatever MySQL error the query generated will now be printed to the screen.

Just a minor note: you should initialize $clean with $clean = array();

If the problem is that the conditional is not firing, then the problem may be elsewhere. Do you have an <input> named "submit" in your form, and is the method of the form post?

Query itself looks okay to me. I think it is difficult to read, so I would do this, but that's just personal style:

$notify = $clean['Notes'] != '' ? '1' : '';
$query = <<<SQL
  INSERT INTO test
     (Notes, EditoryNotify)
  VALUES 
     ($clean[Notes], $notify)

SQL;

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.