If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Problem in mysql statement and if condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-22-10, 02:58
newphpcoder newphpcoder is offline
Registered User
 
Join Date: Dec 2010
Posts: 126
Problem in mysql statement and if condition

Good day!

I have code to check if the cloth type is NW or W for the operation name Operation3, Operation4, and Operation5.

NW = AAA, BBB, CCC
W = all cloth that was not belong to NW

Here is the code I want to ask for you if correct and if I have missing code, because I’m not good in if condition and mysql statement.

Code:
<?php
  include 'config.php';
  
  $cloth_type  = $_POST['clt_type'];
  $input_qty = $_POST['input_qty'];
  $output_qty = $_POST['output_qty'];
  
  if ($_POST["clt_no"]){
      
  $query = "SELECT t.operation_name FROM clt_traceability t, clt_transact c WHERE c.cloth_type = '" .  $cloth_type . "' AND t.operation_name = 'Operation3, Operation4, Operation5 ' AND t.clt_no = c.clt_no";
  $result = mysql_query($query);

  if($cloth_type = 'AAA, BBB ,CCC')  {
    $input_qty = $input_qty * 14.15;
    $output_qty = $output_qty * 14.15;
  }
  else{
    $input_qty = $input_qty * 15.85;
    $output_qty = $output_qty * 15.85;
  }
  }
  
?>


And I attached the code where I want to put the code for checking the cloth type and for the automatic multiply the input qty and output qty depend on the cloth type because the user input numbers by frame so I need to multiply it to become dozen which I put in the test code above.



Thank you.
Attached Files
File Type: txt cmain20.txt (10.6 KB, 17 views)
Reply With Quote
  #2 (permalink)  
Old 12-22-10, 03:00
newphpcoder newphpcoder is offline
Registered User
 
Join Date: Dec 2010
Posts: 126
I try this code:
PHP Code:
<?php
  
include 'config.php';
  
$rexist 0;
if(
$_POST["clt_no"])
    {
    
$query sprintf("INSERT INTO clt_transact(cloth_type)VALUES ('%s')",   
      
mysql_real_escape_string($_POST["cloth_type"])
      );      
      
$result mysql_query($query); 
      
$clt_transact_id mysql_insert_id();
    
    
$opname $_POST["opname"];
    
$inqty $_POST["inqty"];
    
$outqty $_POST["outqty"];
    
$varqty $_POST["varqty"];
   
    
    
$totalarr count($opname) - 1;
    for(
$ctr=0$ctr $totalarr$ctr++)
        {
        
$inqty[$ctr] = (float) $inqty[$ctr];
        
$varqty[$ctr] = (float) $varqty[$ctr];
        
$outqty[$ctr] = (float) $outqty[$ctr];
         
        
$query "INSERT INTO clt_traceability (operation_name, input_qty, output_qty, variance_qty, clt_transact_id) VALUES ('" $opname[$ctr] . "', '" $inqty[$ctr] . "','" $outqty[$ctr] . "', '" $varqty[$ctr] . "', '" $clt_transact_id "')";
        
$result mysql_query($query);
        }   
    }
    
    
$query "SELECT t.operation_name FROM clt_traceability t, clt_transact c WHERE c.cloth_type = '" .  $cloth_type "' AND t.operation_name IN ('Operation3, Operation4, Operation5 ') AND t.clt_no = c.clt_no";
  
$result mysql_query($query);

  if(
$cloth_type == 'AAA' or $cloth_type == 'BBB' or $cloth_type == 'CCC') {
    
$input_qty $input_qty 14.15;
    
$output_qty $output_qty 14.15;
  }
  else{
    
$input_qty $input_qty 15.85;
    
$output_qty $output_qty 15.85;
  }
  
$rexist 0;
?>
Code:
<html>
<head>
    <style type="text/css">
    .myclass {
     font-size: 10pt; font-family:Arial, Helvetica, sans-serif;
    }
    </style>
    
    <script type="text/javascript">
document.onkeypress = function(e){
    e = e || event; e.returnValue = true;
    var t = e.target || e.srcElement, re = /^(inqty|outqty)(\d+)$/, f = arguments.callee, m, i;
    function next(){
        if(!f.els && (m = t.form) === document.forms.clttype){
            var ipts = m.getElementsByTagName('input'), els = []; i = ipts.length - 1;
            for (i; i > -1; --i){
                if(ipts[i].type && ipts[i].type.toLowerCase() === 'text'){
                    els.push(ipts[i]);
                }
            }
            f.els = els;
        }
        if(f.els){
            i = f.els.length - 1;
            for (i; i > -1; --i){
                if(f.els[i] === t && (m = f.els[i - 1])){
                    m.focus();
                }
            }
        }
    }
    if((m = re.exec(t.id)) && e.keyCode === 13){
        e.returnValue = false;
        t.form.elements['varqty' + m[2]].value = t.form.elements['inqty' + m[2]].value - t.form.elements['outqty' + m[2]].value;
    } else if (t.type && e.keyCode === 13 && t.type.toLowerCase() !== 'submit') {
        e.returnValue = false;
    }
    if(!e.returnValue){
        next();
        if(e.preventDefault){e.preventDefault();}
    }
    return e.returnValue;
}
</script>
</head>
<body>
PHP Code:
<form action='frame.php' method="post" name="clttype">

<?php
    $rexist 
1;
    
    echo 
"<table>";
    echo 
"<tr><td>Cloth Type</td><td><input type='text' name='cloth_type' id='cloth_type' /></td></tr>";
    echo 
"</table>";
    echo 
"<p>&nbsp;</p>";
    
    
$query "SELECT * FROM clt_trace_operations ORDER BY operation_name";
    
$last_operation_name "";
    
$result mysql_query($query);
    if(
$result)
        {
        
$rexist 1;
        echo 
"<table>";
        echo 
"<tr><th class='myclass'>OPERATIONS</th><th class='myclass'>INPUT <br/> QTY</th><th class='myclass'>OUTPUT <br/> QTY</th><th class='myclass'>VARIANCE Qty</th></tr>";
        
$totalrows mysql_num_rows($result);
        
$trows $totalrows 1;
        for(
$ctr 0$ctr $trows$ctr++)
            {
            
$row mysql_fetch_array($result);
            
$tctr $ctr 1;
            echo 
"\n\t<tr>";
            echo 
"\n\t\t<td>";
            if (
$last_operation_name != $row["operation_name"]) echo $row["operation_name"];
            
$last_operation_name $row["operation_name"];
            echo 
"<input type='hidden' width='400' name='opname[]' value='" $row["operation_name"] . "' /></td>";  
            echo 
"\n\t\t<td><input size='6' type='text' name='inqty[]' id='inqty" $ctr "' /></td>";
            echo 
"\n\t\t<td><input size='6' type='text' name='outqty[]' id='outqty" $ctr "' ></td>";
            echo 
"\n\t\t<td><input size='6' type='text' name='varqty[]' id='varqty" $ctr "' /></td>";
            echo 
"\n\t</tr>";
            }
        if(
$totalrows 1);
            {
            
$row mysql_fetch_array($result);
            echo 
"\n\t<tr>";
            echo 
"\n\t\t<td>";
            if (
$last_operation_name != $row["operation_name"]) echo $row["operation_name"];
            
$last_operation_name $row["operation_name"];
            echo 
"<input type='hidden' width='400' name='opname[]' value='" $row["operation_name"] . "' /></td>";   
            echo 
"<input type='hidden' name='opname[]' value='" $row["operation_name"] . "' /></td>";
            echo 
"\n\t\t<td><input size='6' type='text' name='inqty[]' id='inqty"  $ctr "' /></td>";
            echo 
"\n\t\t<td><input size='6' type='text' name='outqty[]'  id='outqty" $ctr "'  /></td>";
            echo 
"\n\t\t<td><input size='6' type='text' name='varqty[]' value=''  id='varqty" $ctr "' /></td>";
            echo 
"\n\t</tr>";
            }
        echo 
"</table>";
        }   
           echo 
"<p><input type='submit' value='  Save  ' id='saveform' /></p>";
    echo 
"<input type='hidden' name='clt_typeno' value='" $_POST["clt_typeno"] . "' />";
?>
</form>
</body>
</html>
When I run this code I input BBB in cloth type and I insert numbers in input qty and it did not automatically multiply in 14.15 but no errors encountered. The reason why I need to automatically multiply it because the user input numbers where is the unit of measure is Frame I need to convert it to Dozen so I will multiply to 14.15 if NW and 15.85 if W.
Reply With Quote
  #3 (permalink)  
Old 12-22-10, 04:33
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
how is this a MySQL issue, I don't see any select query retrieving a vlaue for cloth_type?

my suspisicion is that $cloth_type is not populated.

debugging PHP scripts can be a beggar especially if you don't use soemthing like eclipse.

what I'd suggest you do is use a brute force approach and either write the state of variable to a common debug variable, or write the debug bessages to a file and or make frequent use of the die statement.

variable approach
PHP Code:
$debugVar "Cloth Type:".$cloth_type."<BR>";
$debugVar .= "Input Qty:".$input_qty."<BR>";
$debugVar .= "Output Qty:".$output_qty."<BR>";
if(
$cloth_type == 'AAA' or $cloth_type == 'BBB' or $cloth_type == 'CCC') { 
    
$input_qty $input_qty 14.15
    
$output_qty $output_qty 14.15
  } 
  else{ 
    
$input_qty $input_qty 15.85
    
$output_qty $output_qty 15.85
  }
$debugvar .= "-----------------------------------";
$debugVar .= "Input Qty:".$input_qty."<BR>";
$debugVar .= "Output Qty:".$output_qty."<BR>";
die echo 
$debugVar
incidentally I'd strongly advise you not to use magic numbers such as 14.15 or 15.85.even if they are constants and are never ever going to change. if they truly are constants then give 'em a name such as typeblahconversion, although PHP coding standards suggest you should use the define statement and use uppercase

personally I think you should have a table which encodes these conversion factors so that if the user wants to defien a new product type they can do so without any program change. history says the user will often do this sort of thing, then complain to you that your program is broken becuase they've introduced a new product type with a new conversion fact, and invariably its at no notice when you are busy on other things.
so I'd suggest a new table
defining the cloth types, the input conversion factor, and output factor if its ever likely to be different and pull those values in as required
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton

Last edited by healdem; 12-23-10 at 02:56.
Reply With Quote
  #4 (permalink)  
Old 12-22-10, 19:43
newphpcoder newphpcoder is offline
Registered User
 
Join Date: Dec 2010
Posts: 126
I try this code:

PHP Code:
 $query "SELECT t.operation_name FROM clt_traceability t, clt_transact c WHERE c.cloth_type = '" .  $cloth_type "' AND t.operation_name IN ('Operaton3, Operation4, Operation5 ') AND t.clt_no = c.clt_no";
  
$result mysql_query($query);


  if(
$cloth_type == 'AAA' || $cloth_type == 'BBB' || $cloth_type == 'CCC') {
    
$input_qty $input_qty 14.15;
    
$output_qty $output_qty 14.15;
  }
  else{
    
$input_qty $input_qty 15.85;
    
$output_qty $output_qty 15.85;
  } 
can you explain me the used of this code that you given.
PHP Code:
$debugVar "Cloth Type:".$cloth_type."<BR>";
$debugVar .= "Input Qty:".$input_qty."<BR>";
$debugVar .= "Output Qty:".$output_qty."<BR>"
Reply With Quote
  #5 (permalink)  
Old 12-23-10, 01:59
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
thats building a variable showing the state of items in the script
place a die echo $debugVar at some stage to show what your program is doing alternativley use and IDE which supports break points. IIRC eclipse can be set up to do this.

one of the things I miss the most coming from VB or even VBA is the absence of a tightly integrated and flexible debug environment. the earliest php scripts I worked on required a more brute force approach to understand what was going on. its alwasy tough trying to work out why a php script doesn't do what you expect so peppering a script with soemthign like that $debugVar approach is that you can see how things develop over the scripts lifetime

again the code that you are sayign isn't working doens't seem to be based on anythign from the database
my suspiicion is that you are not getting the value in $cloth_type you think you are, hence why I'm suggesting you find what values are there.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 12-23-10, 02:21
newphpcoder newphpcoder is offline
Registered User
 
Join Date: Dec 2010
Posts: 126
presence coppille

Quote:
Originally Posted by healdem View Post
thats building a variable showing the state of items in the script
place a die echo $debugVar at some stage to show what your program is doing alternativley use and IDE which supports break points. IIRC eclipse can be set up to do this.

one of the things I miss the most coming from VB or even VBA is the absence of a tightly integrated and flexible debug environment. the earliest php scripts I worked on required a more brute force approach to understand what was going on. its alwasy tough trying to work out why a php script doesn't do what you expect so peppering a script with soemthign like that $debugVar approach is that you can see how things develop over the scripts lifetime

again the code that you are sayign isn't working doens't seem to be based on anythign from the database
my suspiicion is that you are not getting the value in $cloth_type you think you are, hence why I'm suggesting you find what values are there.
the cloth tyoe is also inputted by the then it will sve in the database
Reply With Quote
  #7 (permalink)  
Old 12-23-10, 02:34
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
fine
so its nothng to do with the db
at this stage I'd argue you need t make certain that the script is doing what you want rather than what you think its doing

one of the madde3ning things about PHP development is that you can convince yourself the code isn't working properly, but in realtiy it doing what you have told it. part of that process is verifying what you are telling the script to do... ie check all inputs

another maddening thing is that php is very forgiving and fault tolerant. so if you don't have the error_reporting set correclty it will blithley continue running even though php has spotted problems in your code.

so check your cloth_typ is set to the values you expect.
you've spotted a problem. given the simplicity of the code if you are not getting the result you expect there is soemthing hooky about the way the scriptis runnign. prime candidate is that you are not setting cloth-type or you are not sendingthe right value to cloth-type. first step for me is prove cloth type is 'BBB', and not 'bbb' or nothing
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #8 (permalink)  
Old 12-23-10, 02:41
newphpcoder newphpcoder is offline
Registered User
 
Join Date: Dec 2010
Posts: 126
How can I test if the cloth type is the cloth type text field?

Thank you
Reply With Quote
  #9 (permalink)  
Old 12-23-10, 02:57
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
the only way I know is to copy the values to a another variable and then examine that variable
hence why In suggesting you create something like $debugVar and display it after your if statement using
die echo $debugVar;
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On