0

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."')";
4
  • 2
    You should print out $sql after you have put in the variables. The answer would then (probably) be obvious. Commented May 5, 2014 at 0:00
  • 1
    Are $course and $chour numbers or strings? Are the corresponding table columns equipped to handle the data you're inserting? If you try to enter a string into an int field, it will go in as 0 Commented May 5, 2014 at 0:08
  • @Gordon Linoff printed $sql=insert into absent (SID,Cid,Tid,Adate,Ahoure) values ('65','','2','2014-05-06','') Commented May 5, 2014 at 0:19
  • @Mark M cid field is int(11) and $course value returned from field cid in another table which have the same type int(11) Commented May 5, 2014 at 1:03

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i echo $course=26 and $chour=2 just before insert query

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.