user3583219
user3583219

Reputation: 1

mysql insert query inserts zeros with some values

I am a beginner in php and mysql and am trying to write code to insert values into a table. The problem is some variables like $upval and $aday are inserted correctly but $course and $chour are inserted as zeros, notice that I echo the ($course and $chour) before the insert query and the echo prints the correct values(not zero).

$res1=mysql_fetch_object($result1);
$course =$res1->cid;
$result2= mysql_query("select thoure from $tbl_name3 where cid='$course'");
$res2=mysql_fetch_object($result2);
$chour =$res2->thoure;
$sql ="insert into $tbl_name2 (SID,Cid,Tid,Adate,Ahoure) values ('$upval','$course','2','$aday','$chour')";
$result = mysql_query($sql);

also I try another way to write query but the same problem

$sql ="insert into $tbl_name2 (SID,Cid,Tid,Adate,Ahoure) values ('$upval','".$res1->cid."','2','$aday','".$res2->thoure."')";

Upvotes: 0

Views: 1993

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

This is your SQL statement:

$sql=insert into absent (SID, Cid, Tid, Adate, Ahoure)
     values ('65','','2','2014-05-06','')

If Cid is being inserted as 0, that is because Cid is a numeric type (probably integer of some sort) and strings are converted to numbers. So, '' is inserted as to 0 into a number. Why the value is set to an empty string, I do not know.

Upvotes: 0

Related Questions