jim
jim

Reputation: 1

PHP - MYSQL trying to store Integer value

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)

Upvotes: 0

Views: 8346

Answers (2)

edorian
edorian

Reputation: 38981

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

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 158003

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";

Upvotes: 4

Related Questions