1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem in mysql statement and if condition

Discussion in 'PHP' started by newphpcoder, Dec 18, 2010.

  1. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    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:

  2. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    I mix my test code and my full code:

    <FONT size=3>
    PHP:
    <?php
     
      
      
      define
    ('CLOTH_TYPE_SPECIAL_VALUE''AAA, BBB, CCC');
      
    define('INPUT_MULTIPLE'14.15);
      
    define('OUTPUT_MULTIPLE'15.85);
      
      include 
    'config.php';
     
    $rexist 0;
    if(
    $_POST["clt_no"])
        {
        
    $query "INSERT INTO clt_transact (cloth_type) VALUES ('" $_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);
            }   
        }
      
      if (isset(
    $_POST['cloth_type']) && !empty($_POST['cloth_type'])) {
            
    $clothType $_POST['cloth_type'];
        } else {
            
    $clothType null;
        }
        
        if (
    null !== $clothType) {
            if (isset(
    $_POST['input_qty']) && !empty($_POST['input_qty'])) {
                 
    $inputQty $_POST['input_qty'];
     
                 if (
    CLOTH_TYPE_SPECIAL_VALUE == $clothType) {
                     
    $inputQty *= INPUT_MULTIPLE;
                 }
            } else {
                 
    $inputQty null;
            }
     
            if (isset(
    $_POST['output_qty']) && !empty($_POST['output_qty'])) {
                 
    $outputQty $_POST['output_qty'];
     
                 if (
    CLOTH_TYPE_SPECIAL_VALUE == $clothType) {
                     
    $outputQty *= OUTPUT_MULTIPLE;
                 }
            } else {
                 
    $outputQty null;
            }
        
      
    $sql "SELECT t.operation_name
                    FROM clt_traceability AS t, clt_transact AS c
                    WHERE c.cloth_type = '" 
    .  $cloth_type "'
                    AND t.operation_name IN('Operation3', 'Operation4', 'Operation5')
                    AND t.clt_no = c.clt_no"
    ;
                    
            
    $resultSet mysql_query($sql);
        }
     
    $rexist 1;
    ?>

    <FONT size=3>
    HTML:
    <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>
    <form action='frame.php' method="post" name="clttype">
    

    PHP:
    <?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>


    and when i run this code and I type AAA as a cloth type and I input 20 in input qty it did not multiply automatically in 14.15, the 20 was not changed.

    Thank you
     
  3. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    I also try this code:
    PHP:
    <?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:
    [/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]<html>[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]<head>[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    <style type="text/css">[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    .myclass {[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]     font-size: 10pt; font-family:Arial, Helvetica, sans-serif;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    </style>[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    [/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    <script type="text/javascript">[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]document.onkeypress = function(e){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    e = e || event; e.returnValue = true;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    var t = e.target || e.srcElement, re = /^(inqty|outqty)(\d+)$/, f = arguments.callee, m, i;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    function next(){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        if(!f.els && (m = t.form) === document.forms.clttype){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            var ipts = m.getElementsByTagName('input'), els = []; i = ipts.length - 1;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            for (i; i > -1; --i){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                if(ipts[i].type && ipts[i].type.toLowerCase() === 'text'){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                    els.push(ipts[i]);[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            f.els = els;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        if(f.els){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            i = f.els.length - 1;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            for (i; i > -1; --i){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                if(f.els[i] === t && (m = f.els[i - 1])){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                    m.focus();[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]                }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]            }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    if((m = re.exec(t.id)) && e.keyCode === 13){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        e.returnValue = false;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        t.form.elements['varqty' + m[2]].value = t.form.elements['inqty' + m[2]].value - t.form.elements['outqty' + m[2]].value;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    } else if (t.type && e.keyCode === 13 && t.type.toLowerCase() !== 'submit') {[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        e.returnValue = false;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    if(!e.returnValue){[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        next();[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]        if(e.preventDefault){e.preventDefault();}[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    }[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]    return e.returnValue;[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]}[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]</script>[/FONT][/SIZE]
    [SIZE=3][FONT=Times New Roman]</head>[/FONT][/SIZE]
    [FONT=Times New Roman][SIZE=3]<body>[/SIZE][/FONT]
    [FONT=Times New Roman][SIZE=3]

    <FONT size=3>
    PHP:
    <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.
     
  4. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    Your query inputs the values before they are multiplied in the database, move the code that multiples the qty code before the query to insert them in the database so that the desired values are entered in the database.
     
  5. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    I try what you said:

    PHP:
     $cloth_type $_POST["cloth_type"];
       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;
      }
        
    $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);
      
    $rexist 0;
    But still it did not multiply automatically

    Thank you
     
  6. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    You did not insert it into the database. See the problem is you added the if statements that did the math after the data was already added to the data base. Try ouputting the vaules to the browser to check that your values match what you want added to the database. Also make sure that the post value actually has some sort of value in it by making a sample page. Let me show you what I mean give me a second to write an example for you ok and I'll update this post with the example and screen shots of it working ok.
     
    newphpcoder likes this.
  7. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    Sorry edit time limit reach also try out this code I've made sure it worked with the form and change the db fields to your own those are mine for one of my test servers at home(I use xampp, zend server, and wamp on one system).

    Heres my html form its fairly basic but its just to show you how to work it.


    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
    
    <body>
    
    <form method="post" action="process.php">
    Select A Cloth Type<br />
    <select name="cloth_type">
    <option value="AAA">AAA</option>
    <option value="BBB">BBB</option>
    <option value="CCC">CCC</option>
    </select>
    <br />
    Enter the Quantity<br />
    <input type="text" name ="qty" />
    <br />
    
    <input type="reset" value="clear" /> &nbsp; <input type="submit" value="send" />
    </form>
    </body>
    </html>
    Now you need to create a new database run the query below in either the mysql.exe cmd prompt or in phpmyadmin

    Code:
    CREATE DATABASE example;
    
    USE example;
    
    CREATE TABLE currency (id INT NOT NULL AUTO_INCREMENT,cloth_type VARCHAR(3) NOT NULL,qty INT NOT NULL,total FLOAT NOT NULL, PRIMARY KEY(id));
    Now Here is my php code to process the above html form and output that the POST vars are set and enter them in the data base. Normally you would use a cck-ish style so you have auto code that would process the form so you could make new forms via an admin panel that did not require you to write code to process them. See form generator in google for help with that and also I did not stop execution of the code if there was no db connection which means it will still attempt the queries even though no db connection was made.

    PHP:
    <?php

    $cloths 
    $_POST['cloth_type'];
    $qty $_POST['qty'];
    $pay;

    $dbHost "localhost";
    $dbUser "pein87";
    $dbPass "daking";

    $DB_CONN = @mysql_connect($dbHost,$dbUser,$dbPass);
    if(!
    $DB_CONN)
    {

        echo 
    "error connecting to database" "<br />";

    }
    $table = @mysql_select_db("example");
    if(!
    $table)
    {

        echo 
    "no table was selected" "<br />";

    }

    echo 
    $cloths;
    echo 
    "<br />";
    echo 
    $qty;
    echo 
    "<br />";

    if(
    $cloths// cloths is not blank
    {

    switch(
    $cloths)
    {

        case 
    "AAA":
            
    $pay 14.15;
            
    $amt $qty $pay;
            echo 
    "You've orders " $qty " @" "\$" $pay " a piece.";
            echo 
    "<br />  Your Total is " "\$" $amt;
            
    $Q = @mysql_query("INSERT INTO currency (cloth_type,qty,total) VALUES ('".$cloths."',$qty,$amt)"$DB_CONN);
            if(
    $Q)
            {
            
                echo 
    "<br />" "Your info has been entered in the database.";
            
            }
            else
            {
            
                echo 
    "<br />" "there was a problem adding your data to the database.";
            
            }
            break;
        case 
    "BBB":
            
    $pay 14.15;
            
    $amt $qty $pay;
            echo 
    "You've orders " $qty " @" "\$" $pay " a piece.";
            echo 
    "<br />  Your Total is " "\$" $amt;
            
    $Q = @mysql_query("INSERT INTO currency (cloth_type,qty,total) VALUES ('".$cloths."',$qty,$amt)"$DB_CONN);
            if(
    $Q)
            {
            
                echo 
    "<br />" "Your info has been entered in the database.";
            
            }
            else
            {
            
                echo 
    "<br />" "there was a problem adding your data to the database.";
            
            }
        break;
        case 
    "CCC":
            
    $pay 15.85;
            
    $amt $qty $pay;
            echo 
    "You've orders " $qty " @" "\$" $pay " a piece.";
            echo 
    "<br />  Your Total is " "\$" $amt;
            
    $Q = @mysql_query("INSERT INTO currency (cloth_type,qty,total) VALUES ('".$cloths."',$qty,$amt)"$DB_CONN);
            if(
    $Q)
            {
            
                echo 
    "<br />" "Your info has been entered in the database.";
            
            }
            else
            {
            
                echo 
    "<br />" "there was a problem adding your data to the database.";
            
            }
        break;
        default:
            echo 
    "<br />" "No Item was Selected please select and item.";
        break;

    }

    }

    ?>
    See how it is multiplying the $qty before the $pay before its being added to the database? For reference you need to add a shopper id and purchase id so that you can get that info or you'll need to use

    PHP:
    $tot_rows = @mysql_query("SELECT COUNT(*) FROM currency"$DB_CONN);

    $last_row = @mysql_query("SELECT * FROM currency LIMIT $tot_rows, 1"$DB_CONN);

    while(
    $rowArray = @mysql_fetch_assoc($last_row))
    {

    echo 
    "ID: " $rowArray[id] . " cloth type: " $rowArray[cloth_type] . " Quantity: " $rowArray[qty] . " Total Cost: " $rowArray[total];

    }
    Heres how it would look in an if condition statement

    PHP:
    <?php 

    $cloths 
    $_POST['cloth_type']; 
    $qty $_POST['qty']; 
    $pay

    $dbHost "localhost"
    $dbUser "pein87"
    $dbPass "daking"

    $DB_CONN = @mysql_connect($dbHost,$dbUser,$dbPass); 
    if(!
    $DB_CONN


        echo 
    "error connecting to database" "<br />"


    $table = @mysql_select_db("example"); 
    if(!
    $table


        echo 
    "no table was selected" "<br />"



    echo 
    $cloths
    echo 
    "<br />"
    echo 
    $qty
    echo 
    "<br />"

    if(
    $cloths// cloths is not blank 
    {
    if (
    $cloths == "AAA" || $cloths == "BBB")
    {

    $pay 14.15
            
    $amt $qty $pay
            echo 
    "You've orders " $qty " @" "\$" $pay " a piece."
            echo 
    "<br />  Your Total is " "\$" $amt
            
    $Q = @mysql_query("INSERT INTO currency (cloth_type,qty,total) VALUES ('".$cloths."',$qty,$amt)"$DB_CONN); 
            if(
    $Q
            { 
             
                echo 
    "<br />" "Your info has been entered in the database."
             
            } 
            else 
            { 
             
                echo 
    "<br />" "there was a problem adding your data to the database."
             
            }

    }
    else if(
    $cloths == "CCC")
    {

    $pay 15.85
            
    $amt $qty $pay
            echo 
    "You've orders " $qty " @" "\$" $pay " a piece."
            echo 
    "<br />  Your Total is " "\$" $amt
            
    $Q = @mysql_query("INSERT INTO currency (cloth_type,qty,total) VALUES ('".$cloths."',$qty,$amt)"$DB_CONN); 
            if(
    $Q
            { 
             
                echo 
    "<br />" "Your info has been entered in the database."
             
            } 
            else 
            { 
             
                echo 
    "<br />" "there was a problem adding your data to the database."
             
            }

    }
    else
    {

    echo 
    "No Cloth Value selected please select a cloth type.";

    }
    }
    ?>
    Last pointer make sure to check to make sure that the user submitted values are the data type you want. For numbers use

    PHP:
    if(!is_numeric($myVar))
    {

    echo 
    "not a number";

    }
    else
    {

    echo 
    "is a number";

    }
    If you dont check the data you can get hacked and also strip tags, html special characters, and possibly strip out script tags. You can use built in functions for this to.

    PHP:
    function clean($str)
    {

    strip_tags($str); // strip html tags

    htmlspecialchars($str); //turns them into there form so & would be &amp;

    return $str;
    }
     
    Last edited: Dec 30, 2010

Share This Page