tags:

views:

43

answers:

3

i am using following code to add data into mysql database

<?php
$debdes  = $_POST['debdes'];
$debamt  = $_POST['debamt'];
$crdes   = $_POST['crdes'];
$cramt   = $_POST['cramt'];
$date    = $_POST['date'];
$cberror = "<h1>Data not Added</h1><br/><br/><h3>Please Follow The Instructions</h3>";

include_once ("db.php");



if (empty ($date)) die ("$cberror");

if (empty ($debamt) && empty ($debdes) && empty ($cramt) && empty ($cramt)) die ("$cberror");

if (!empty($debamt) && empty ($debdes))die ("$cberror");

if (!empty($debdes) && empty ($debamt)) die ("$cberror");


if (!empty($cramt) && empty ($crdes)) die ("$cberror");

if (!empty($crdes) && empty ($cramt)) die ("$cberror");

 $ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";

if(mysql_query("$ucbook"))

{
echo "<h3>One Record Updated Successfully with the following Details </h3><br/>";
echo "Date = $date <br/><br/>";
echo "Debt Amount =$debamt <br/><br/>";
echo "Debt Description = $debdes <br/><br/>";
echo "Credit Amount = $cramt <br/><br/>";
echo "Credit Description = $crdes <br/><br/>";

}
else 
{
echo "<be/><br/>The Following Error Occure While Updatig Data<br/><br/>";
echo mysql_error(); 
}

?>

now the problem is that as i set the value of "debamt" as integer in mysql then error comes

Incorrect integer value: '' for column 'debamt' at row 1 

although i did not insert value in "debamt" i am also filling other data correctly

??????

+2  A: 

That means that debamt was empty. Empty is not a correct integer value.

You know, you could also use the || logical operator, which means OR. Because this row:

if (empty ($debamt) && empty ($debdes) && empty ($cramt) && empty ($cramt)) die ("$cberror");

...means that if even one of those values is NOT empty, the if clause will not fire. What you probably want is:

if (empty ($debamt) || empty ($debdes) || empty ($cramt) || empty ($cramt)) die ("$cberror");

That way, if one of the fields is empty, the script will not continue. This also makes a lot of your consequent checks useless.

Because of your current code, it is completely possible that $debamt gets through as empty. I'd engourage you to think your if clauses through and see what's really necessary.

Tatu Ulmanen
Also, the code is vulnerable to SQL injection attacks - the strings are not escaped.
Lucas Jones
@Lucas Jones, Yep, I just didn't want to mix OP's head with that until he graspes the basic concepts of PHP syntax :)
Tatu Ulmanen
A: 

From your code it seems that it is still possible for $debamt to be empty, so your sql statement is trying to insert an empty argument. '' is not a valid integer

ctshryock
A: 

An empty string is not a valid integer. Either you must provide a proper number of you should declare the column to allow nulls (and pass a null rather than an empty string).

You should also read up on SQL injection.

Dan Dyer