Page 1 of 1

Insert into mysql condition checkbox

Posted: Thu Jan 08, 2009 4:20 am
by JimiH
Hello I have the following code which insert values into two mysql tables "projects" and "project_comments"

Code: Select all

$sql="update projects set VSI_NUMBER='$VSI_NUMBER',Salesman='$Salesman',ENGINEER ='$ENGINEER',Prod_Spec ='$Prod_Spec'
            ,CUSTOMER ='$CUSTOMER',SALESGROUP ='$SALESGROUP',SICCODE ='$SICCODE',Conf_fact ='$Conf_Fact',N_E ='$N_E'
            ,YEARQTY ='$YEARQTY',CHCKLRATIO ='$CHCKLRATIO',VALUEExworks ='$VALUEExworks',COMPET ='$COMPET', DATEIN ='$DATEIN'
            ,COMMENTS ='$COMMENTS',ACTIONDATE ='$ACTIONDATE',Project_Members ='$Project_Members'
            ,Gate ='$Gate',Job_No ='$Job_No',Cata_Number ='$Cata_Number',orderrec ='$orderrec',Produced_at ='$Produced_at'
            ,Status ='$Status',Suspend ='$Suspend',product = '$Product',Comm_Memb ='$Comm_Memb',ECR_NUMB = '$ECR_NUMB' where VSI_NUMBER='$VSI_NUMBER'";
          
$sql1="insert into project_comments(ID,Comments,ActDate,Proj_Member)
 
 
I have a conditional checkbox on my input form that when unchecked I want it to not insert the second query "$sql1" so I created an "If" condition (Below)

Code: Select all

$sql="update $branch set VSI_NUMBER='$VSI_NUMBER',Salesman='$Salesman',ENGINEER ='$ENGINEER',Prod_Spec ='$Prod_Spec'
            ,CUSTOMER ='$CUSTOMER',SALESGROUP ='$SALESGROUP',SICCODE ='$SICCODE',Conf_fact ='$Conf_Fact',N_E ='$N_E'
            ,YEARQTY ='$YEARQTY',CHCKLRATIO ='$CHCKLRATIO',VALUEExworks ='$VALUEExworks',COMPET ='$COMPET', DATEIN ='$DATEIN'
            ,COMMENTS ='$COMMENTS',ACTIONDATE ='$ACTIONDATE',Project_Members ='$Project_Members'
            ,Gate ='$Gate',Job_No ='$Job_No',Cata_Number ='$Cata_Number',orderrec ='$orderrec',Produced_at ='$Produced_at'
            ,Status ='$Status',Suspend ='$Suspend',product = '$Product',Comm_Memb ='$Comm_Memb',ECR_NUMB = '$ECR_NUMB' where VSI_NUMBER='$VSI_NUMBER'";
 
 
 if ( $commupdate == 'on' ) {           
            
$sql1="insert into project_comments(ID,Comments,ActDate,Proj_Member)
 
 values('$VSI_NUMBER','$COMMENTS','$ACTIONDATE','$Comm_Memb')";
 
 }
However when I uncheck the box I get a "Query was empty" error via

Code: Select all

echo '<pre>sql: '; print_r($sql); echo "\n</pre>\n"; 
 
If I leave the box checked both queries operate successfully which is correct

Can anyone see any claring errors?

Thanks

Geoff

Re: Insert into mysql condition checkbox

Posted: Thu Jan 08, 2009 6:14 am
by jaoudestudios
Echo the queries ($sql) to screen with the checkbox checked and un-checked. It will be easier for us to spot the mistake.

Re: Insert into mysql condition checkbox

Posted: Thu Jan 08, 2009 7:35 am
by JimiH
Finally sorted it by commenting the below line out, dont know what it was doing there but
it was causing the page to display "Query was empty" error, due to $sql1 being empty

Code: Select all

//$result=mysql_query($sql1,$connection) or die(mysql_error());
Thanks

Re: Insert into mysql condition checkbox

Posted: Thu Jan 08, 2009 8:54 am
by jaoudestudios
That line will run your $sql1 query. So I am sure you still need that line, but there is an error in the query sent to it $sql1. It would return empty as it would return false because mysql server would not run the query.

Have you solved your problem? :?

If not, post your queries with example data not php