Jump to content

Barand

Moderators
  • Posts

    24,344
  • Joined

  • Last visited

  • Days Won

    795

Posts posted by Barand

  1. Your db connection code needs adjustment

    //make connection to DB server
    
        // call mysql_report so that all errors are reported as execptions
        // this saves you from having to check every mysql function call to see if it worked or not
    
        mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);    
        $connS = mysqli_connect($servernameS, $usernameS, $passwordS, $databaseS);

    Now you can remove your code which checks for errors and replace your insert query with the coed I gave you

  2. Use try .. catch, for example

    // pseudocode - in case you didn't notice)...
    
    try {
       insert the record                                 // attemp the insert
    }
    catch (exception) {
       if (exception error code is 1062) {               // error detected - was it a duplicate?
           output your duplicate record message          //   yes it was so report it
       } else {
           throw (exception)                             // no it wasn't so let php handle the exception
       }
    }

     

  3. Something like...

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Example</title>
        <meta charset="utf-8">
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script type='text/javascript'>
            $(function() {
                $(".form-button").click(function(e) {
                    e.preventDefault()                            // stop button from submitting the form
                    alert("Button " + $(this).val() + " clicked")
                })
            })
        </script>
        <style type='text/css'>
            #myform {
                padding: 30px;
                width: 330px;
                margin: 20px auto;
                border: 1px solid blue;
                text-align: center;
            }
        </style>
    </head>
    <body>
    
        <form id='myform' >
            <button class='form-button' name='form-button' value='1'>Button 1</button>
            <button class='form-button' name='form-button' value='2'>Button 2</button>
            <button class='form-button' name='form-button' value='3'>Button 3</button>
            <br><br>
            <button>Submit</button>
        </form>
    </body>
    </html>

     

  4. Is this the effect you are looking for?

    image.png.d1ebab223f6bc422cb5c53213bd0cd83.png

    <head>
        <style type='text/css'>
            .curved-bottom {
                clip-path: ellipse(100% 60% at 50% 40%);
                background-color: #FFFF00;
                color: black;
            }
            .w3-row {
                background-color: black;
                color: white;
            }
            .w3-col {
                padding: 50px 0;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class='w3-row'>
            <div class='w3-col m12 curved-bottom'>
                I have a curvy bottom
            </div>
        </div>
        <div class='w3-row'>
            <div class='w3-col m12'>
                I'm a straight guy
            </div>
        </div>
    </body>

     

    • Like 1
  5. The only MyIsam-only  functionality that I can think of is the ability to have a compound primary key EG

    PRIMARY KEY (year, number)

    where the 2nd part auto_increments within the first part, so if you have

    CREATE TABLE `test1` (
      `year` int(11) NOT NULL,
      `number` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`year`,`number`)
    ) ENGINE=MyISAM ;
    
    mysql> select * from test1;
    +------+--------+
    | year | number |
    +------+--------+
    | 2022 |      1 |
    | 2022 |      2 |
    +------+--------+
    
    
    mysql> insert into test1 (year) values (2022), (2022), (2023), (2023), (2024);
    
    mysql> select * from test1;
    +------+--------+
    | year | number |
    +------+--------+
    | 2022 |      1 |
    | 2022 |      2 |
    | 2022 |      3 |
    | 2022 |      4 |
    | 2023 |      1 |
    | 2023 |      2 |
    | 2024 |      1 |
    +------+--------+
    
     

     

    • Like 2
  6. If this is your insert query...

    INSERT INTO `Customer` (`SyncroID`, `PrimaryContactID`, `CompanyName`, `Address`, `City`, `State`, `Postcode`, `Country`, `AccountStatus`, `UserName`, `Password_Hash`, `Password_Salt`, `Notes`, `BillingContactID`)
    VALUES ('12321', NULL, 'test', NULL, NULL, 'QLD', NULL, 'Australia', 4, NULL, NULL, NULL, NULL, NULL);

    ...there is only a 50% correlation between those column names and the data keys in the customer array...

    -------------------------------------+----------------------------------------- 
    |         Matched                    |           Unmatched                    |
    -------------------------------------+----------------------------------------- 
     $customer["id"];                        $customer["firstname"];    
                                             $customer["lastname"];               
     $customer["business_name"];             $customer["email"];                  
     $customer["address"];                   $customer["phone"];                  
     $customer["city"];                      $customer["mobile"];
     $customer["state"];                     $customer["business_and_full_name"]; 
     $customer["zip"];                       $customer["business_then_name"];     
                                             $customer["fullname"];

     

  7. Firstly, these lines of code do absolutely nothing...

        $customer["id"];
        $customer["firstname"];
        $customer["lastname"];
        $customer["fullname"];
    
        $customer["business_name"];
        $customer["email"];
        $customer["phone"];
        $customer["mobile"];
        $customer["address"];
        $customer["city"];
        $customer["state"];
        $customer["zip"];
        $customer["business_and_full_name"];
        $customer["business_then_name"];
        

    Secondly, you are using the least efficient means of inserting data that there is. Single inserts are slow. Marginally faster are prepared statements where you prepare once then execute in a loop with new value for each call. By far the fastest way is use multiple inserts.

    EG INSERT INTO table (id, name) VALUES (1, 'Name A'), (2, 'Name B'), (3, 'Name 3');

    To implement...

    $data = [];
    foreach ($customers as $c)  {
        $data[] = sprintf("( %d, '%s')", $c['id'], $c['business_name']); 
    }
    $pdo->exec("INSERT INTO Customer (SyncroID, CompanyName) VALUES " . join(',', $data));

     

  8. @Senthilkumar is this any faster than yours?

    It should get all the data you need in a single query.

    <?php
    $sql = "SELECT c.branch
                 , c.printed
                 , c.pending
                 , c.cancelled
                 , c.over7days
                 , c.reminder
                 , y.year
                 , y.yrtotal
                 , y.yrcount
                 , m.month
                 , m.mthtotal
                 , m.mthcount
            FROM (     
                     SELECT c.branch
                         , sum(status=1) as printed
                         , sum(status=0) as pending
                         , sum(status=2) as cancelled
                         , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days
                         , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder
                     FROM calibrationdata1 c
                          JOIN userdetails1 u USING (branch)
                     WHERE u.id = ?
                     GROUP BY branch
                 ) c
                 JOIN (
                        SELECT branch
                             , date_format(date, '%Y') as year
                             , sum(amount) as yrtotal
                             , count(*) as yrcount
                        FROM calibrationdata1
                        GROUP BY branch, year(date)
                      ) y USING (branch)
                 JOIN (
                        SELECT branch
                             , date_format(date, '%b') as month
                             , sum(amount) as mthtotal
                             , count(*) as mthcount
                        FROM calibrationdata1
                        WHERE year(date) = year(curdate())
                        GROUP BY branch, month(date)
                      ) m USING (branch)
           ";
    $res = $pdo->prepare($sql);                                  // NOTE: PDO connection in use
    $res->execute([ $_SESSION['id'] ?? 16 ]);
    $counts = [];
    $yrdata = [];
    $monthdata = [];
    foreach ($res as $row) {
        $counts = array_slice($row, 0, 6);
        $yrdata[$row['year']] = array_slice($row, 6,3);
        $monthdata[$row['month']] = array_slice($row, 9);
    }
    
    echo "<table style='width: 600px; margin: 20px; text-align: center'>" .
         "<tr><th>" . join('</th><th>', array_keys($counts)) . "</th></tr>" .
         "<tr><td>" . join('</td><td>', array_values($counts)) . "</td></tr>" .
         "</table>";
    
    echo printArray($yrdata);
    echo printArray($monthdata);
    
    
    function printArray($arr)
    {
        $out = "<table style='width: 200px; margin: 20px; text-align: center'>" .
               "<tr><th>" . join('</th><th>', array_keys(current($arr))) . "</th></tr>";
        foreach ($arr as $a) {
               $out .= "<tr><td>" . join('</td><td>', array_values($a)) . "</td></tr>";
        }
        $out .= "</table>\n";
        return $out;
    }
    
    ?>

    Example output

    image.png.38d26995a7cb1fa41574a688b0a9b3eb.png

    • Like 1
  9. Yess Phill, you are correct but there will only be a single branch.

    My original query was

    SELECT branch
         , sum(status=1) as printed
         , sum(status=0) as pending
         , sum(status=2) as cancelled
         , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days
         , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder
    FROM calibrationdata c
    WHERE branch = ?

    but I changed it on posting to demonstrate that he should be joining to the userdetails table intead of the first query to get the branch

  10. Try this to replace your first 6 queries

    SELECT u.branch
                 , sum(status=1) as printed
                 , sum(status=0) as pending
                 , sum(status=2) as cancelled
                 , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days
                 , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder
            FROM calibrationdata c
                 JOIN userdetails u USING (branch)
            WHERE u.id = 16;
            
    +--------+---------+---------+-----------+-----------+----------+
    | branch | printed | pending | cancelled | over7days | reminder |
    +--------+---------+---------+-----------+-----------+----------+
    | Mumbai | 8       | 3       | 0         | 3         | 3        |
    +--------+---------+---------+-----------+-----------+----------+

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.