1

I am getting 'Incorrect integer value: '' for column country_id'. Sometimes my dropdown is hidden in the form. So I am not sure how to handle this situtation. Here is my code. Thanks for any help.

$countryId = isset($_POST['country']) ? $_POST['country'] : 0;

$inserSQL = "INSERT INTO Table1(country_id) VALUES('" .$countryId. "')";

$Result1 = mysql_query($inserSQL ) or die(mysql_error());
2
  • 1
    A little bit obvious but...where's the double or single quotes around the Sql statement? Commented Dec 8, 2011 at 18:27
  • Look ant the EmCo answer. it has everything you need Commented Dec 8, 2011 at 18:59

2 Answers 2

6

You are adding ' to the $countryId value. Since an integer is expected, you don't have to use them. Try this:

$countryId = isset($_POST['country']) ? (int)$_POST['country'] : 0;

$inserSQL = "INSERT INTO Table1(country_id) VALUES($countryId)";

$Result1 = mysql_query($inserSQL ) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

1 Comment

The real issue is not the quotes around the data but the fact that $_POST['country'] may come back blank or empty. MySQL will interpret '' as an empty STRING not an int whereas '0' it will recognize and auto-convert to an int. This code works because of the (int) typecast on $_POST['country'] which turns (int)'' into 0.
0

I once experienced this problem and apparently i had a typo instead of putting a 0 i typed an O "Letter O" and that value was not expected in the database because the database field only accepted integers.

Make sure in your the value being passed from your code is integer type.

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.