Insert into mysql condition checkbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Insert into mysql condition checkbox

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Insert into mysql condition checkbox

Post 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.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Re: Insert into mysql condition checkbox

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Insert into mysql condition checkbox

Post 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
Post Reply