开发者

mysql integer problem

i am using following code to add data into mysql database

<?php
$debdes  = $_POST['de开发者_运维知识库bdes'];
$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

??????


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜