0

When I try to execute this is AND getting the following error:

Error: Incorrect integer value: 'Age' for column 'age' at row 1

MYSQL --> I have created a database abc inside created a table with name ee and in the table i have created a field "age", type - "INT" , Length - 10 , Default - NULL

Can SOME ONE PLEASE HELP WHY I AM GETTING THIS ERROR - Incorrect integer value: 'Age' for column 'age' at row 1

<html>
    <body>
        <form action="test5.php" method="post">
            Age: <input type="text" name="age" />
            <input type="submit" />
        </form>
    </body>
</html> 

test5.php ( Page )

$Age= $_POST[ age ] ;

echo $age;

$con = mysql_connect("localhost","root","***");
if( !$con )
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("abc", $con);
$sql = "INSERT INTO ee (Age) VALUES ('Age')";

if( !mysql_query( $sql, $con ) )
{
    die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
1
  • Regardless of the question; Please try to apply some input validation, your code is vunerable to SQL Injections. Commented Mar 3, 2011 at 15:00

2 Answers 2

4

you're inserting word "Age", not variable $age
you also ought to sanitize this variable.

$age = intval($_POST['age']);
echo $age;

$con = mysql_connect("localhost","root","vvvrks"); 
mysql_select_db("abc", $con);

$sql = "INSERT INTO ee (Age) VALUES ('$age')";
mysql_query($sql,$con) or trigger_error(mysql_error()." ".$sql);
echo "1 record added";
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to store the users age as an integer you should change your query:

$sql="INSERT INTO ee (Age) VALUES ('Age')";

to

$Age = (int)$_POST['Age'];

$sql="INSERT INTO ee (Age) VALUES ($Age)"; 

*note the string cast, you should always filter those values to prevent against sql-injection

The error message itself:

Mysql is telling you that it can't store the string "Age" in an integeter field. You want to store the contents of the variable $Age

1 Comment

Please rewrite to: $sql = sprintf( 'INSERT INTO ee (Age) VALUES( %d )', $Age );

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.