Jump to content

Barand

Moderators
  • Posts

    24,572
  • Joined

  • Last visited

  • Days Won

    824

Posts posted by Barand

  1. Earlier in this thread you said that this ...

    +----+-----------+-----------+
    | id | slug      | tag_check |
    +----+-----------+-----------+
    |  1 | hugh-jass |      1    |
    |  2 | hugh-jass |      2    |
    +----+-----------+-----------+

    ... was acceptable. Your "solution" would not permit this.

    Secondly, you are using a "dependent subquery" which means for every record inserted you must query all the existing lookig for a duplicate. This can be extremely slow and should be avoided when writing queries.

    Changing your unique key to ...

    UNIQUE INDEX `unq_name` (`name`) USING BTREE,

    ... and using this query would have same result ...

    INSERT IGNORE into wp_terms (name,slug)
    SELECT concat(nameFirst,' ',nameLast)
         , lower(concat(nameFirst,'-',nameLast))
    FROM a_players
    ;

     

    • Great Answer 1
  2. Is it necessary to include the tag_check in the unique key?

    Would this be acceptable...

    +----+-----------+-----------+
    | id | slug      | tag_check |
    +----+-----------+-----------+
    |  1 | hugh-jass |      1    |
    |  2 | hugh-jass |      2    |
    +----+-----------+-----------+

    or can there be only one "hugh-jass" in the table?

  3. Your UNIQUE key is the combination of (slug, tag_check). NULL values are ignored and your insert query always writes NULL to tag_check column.

    CREATE TABLE `dupe_test` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `slug` varchar(50) NOT NULL DEFAULT '',
      `tag_check` tinyint(4) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `player_tag_check` (`slug`,`tag_check`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

     

    select * from dupe_test;
    
    +----+-----------+-----------+
    | id | slug      | tag_check |
    +----+-----------+-----------+
    |  1 | hugh-jass |      NULL |
    |  2 | hugh-jass |      NULL |
    |  3 | hugh-jass |      NULL |
    |  4 | hugh-jass |         1 |
    +----+-----------+-----------+

    Add another...

    MariaDB [test]> insert into dupe_test (slug) values ('hugh-jass');
    Query OK, 1 row affected (0.051 sec)
    
    MariaDB [test]> select * from dupe_test;
    +----+-----------+-----------+
    | id | slug      | tag_check |
    +----+-----------+-----------+
    |  1 | hugh-jass |      NULL |
    |  2 | hugh-jass |      NULL |
    |  3 | hugh-jass |      NULL |
    |  6 | hugh-jass |      NULL |  << NEW
    |  4 | hugh-jass |         1 |
    +----+-----------+-----------+
    5 rows in set (0.000 sec)

    Now try adding again but with tag_check = 1 then 2...

    MariaDB [test]> insert into dupe_test (slug, tag_check) values ('hugh-jass', 1);
    ERROR 1062 (23000): Duplicate entry 'hugh-jass-1' for key 'player_tag_check'
    
    MariaDB [test]> insert into dupe_test (slug, tag_check) values ('hugh-jass', 2);
    Query OK, 1 row affected (0.073 sec)
    
    MariaDB [test]> select * from dupe_test;
    +----+-----------+-----------+
    | id | slug      | tag_check |
    +----+-----------+-----------+
    |  1 | hugh-jass |      NULL |
    |  2 | hugh-jass |      NULL |
    |  3 | hugh-jass |      NULL |
    |  6 | hugh-jass |      NULL |
    |  4 | hugh-jass |         1 |
    |  8 | hugh-jass |         2 |
    +----+-----------+-----------+
    6 rows in set (0.000 sec)

    So either

    • insert a tag_check value too, or
    • exclude tag_check from the UNIQUE key
    • Great Answer 1
  4. 3 hours ago, radarman2000 said:

    Making any sense?

    Not a lot. "It doesn't work" tells us nothing. What is ir doing that it shouldn't or what is it not doing that it should?

    • If element_5 contains a price, why not call it "price". Same goes for element_6 (option_id).
    • I can't see what do_query() is doing but as you're passing an array of parameters I assume you are trying to use a prepared statement - but you have no placeholders in the query string for those parameters.
    • If $filter_keyword contains a column name that could be the cause - you can only pass values as parameter.
    • If any of your joins match more than 1 row in any of the tables, the resultant sum would be multiplied by the number of rows.

    If you need more help, a dump of the structures and data for those tables would enable me to recreate the problem at my end a get you a working query.

  5. You need to unserialise the data.

    $dbdata = 'a:5:{s:16:"WPFormsDB_status";s:6:"unread";s:4:"Name";s:13:"Lional Hewitt";s:14:"Contact Number";s:10:"0763229844";s:5:"Email";s:22:"[email protected]";s:18:"Comment or Message";s:5:"test2";}';
    $data = unserialize($dbdata);
    
        echo "Name : {$data['Name']}<br>";
        echo "Contact : {$data['Contact Number']}<br>";
        // etc

    Alternatively

    $dbdata = 'a:5:{s:16:"WPFormsDB_status";s:6:"unread";s:4:"Name";s:13:"Lional Hewitt";s:14:"Contact Number";s:10:"0763229844";s:5:"Email";s:22:"[email protected]";s:18:"Comment or Message";s:5:"test2";}';
    $data = unserialize($dbdata);
    
    foreach ($data as $key => $value)   {
        echo "<b>$key</b> : $value<br>";
    }
  6. Here's an example. As you enter more letters of the name it narrows down the resulting list.

    image.png.c304e788e1385b3813e777836907bb2f.pngimage.png.5453efefc164369b30a450e5d8951e5f.png

    (first and last names are searched)

    <?php
    #
    # HANDLE AJAX REQUESTS
    #
        if (isset($_GET['ajax'])) {
            if ($_GET['ajax'] == 'names')  {
                $res = $pdo->prepare("SELECT id
                                         , concat(fname, ' ', lname) as name
                                      FROM person
                                      WHERE fname LIKE ? OR lname LIKE ?
                                      ORDER BY fname, lname     
                                     ");
                $res->execute([ $_GET['search'], $_GET['search'] ]);
                $results = $res->fetchAll();
                exit(json_encode($results));
            }
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Example</title>
    <meta charset="utf-8">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <style type='text/css'>
    </style>
    <script type='text/javascript'>
        $(function() {
            
            $("#search").on("input",function() {
                let srch = $(this).val() + "%"
                $.get(
                    "",
                    {"ajax":"names", "search":srch},
                    function(resp)  {
                        $("#person").html("")
                        $.each(resp, function(k,v) {
                            let opt = $("<option>", {"value":v.id, "text":v.name});
                            $("#person").append(opt);
                        })
                    },
                    "JSON"
                )
            })
        })
    </script>
    </head>
    <body>
        <form>
            Enter first part of name: 
            <input type='text' id='search' >
            <br><br>
            <select name='person_id' id='person' size='20'>
                 <!-- names returned from search go here -->
            </select>
    
        </form>

     

    • Great Answer 1
  7. 1) Do you have php's error reporting ON and error level set to E_ALL?

    2) Have you turned mysql's error reporting on with

     mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

    immediately before your call to

    $con = mysqli_connect( ... )

     

  8. 7 hours ago, mac_gyver said:

    you would ORDER BY the id (autoincrement primary index) column instead.

    But only if you can guarantee that the oldest records are always added first. If relative age is important why would you not store the date added?

    Having a date also has the advantage that you can have the option to
     

    DELETE FROM tablename
    WHERE datecol < CURDATE() - INTERVAL ? DAY

    so you can delete all those over N days old

  9. Is this any use to you?

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Wordle Assist</title>
    <meta charset="utf-8">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <style type='text/css'>
        @media print {
            .printme {
                visibility: visible;
                display: block;
            }
            .noprint {
                visibility: hidden;
            } 
        }
        #print {
            font-family: "times new roman", serif;
            font-size: 20pt;
        }
        
        #address {
            margin: 50px;
            line-height: 40px;
        }
    
    </style>
    <script type='text/javascript'>
        $(function() {
            if ($("#address").text() != '')   {
                print()
            }
        })
    </script>
    </head>
    <body>
        <div id='address' class='printme'>
            <?= nl2br($_GET['addy'] ?? '') ?>
        </div>
        
        <header class='noprint'>
            <h1>Address Labels</h1>
        </header>
        
        <form class='noprint'>
            Address:<br><br>
            <textarea cols='60' rows='5' name='addy'></textarea>
            <br><br>
            <button id=btnPrint'>Print</button>
        </form>
        
    </body>
    </html>

     

  10. You only need one query. For example

    TABLE : product
    +----+-------------+--------------+--------+
    | id | productName | category     | status |
    +----+-------------+--------------+--------+
    |  1 | Room 1      | Guestroom    | Active |
    |  2 | Room 2      | Guestroom    | Active |
    |  3 | Room 3      | Guestroom    | Active |
    |  4 | Room 4      | Guestroom    | Active |
    |  5 | Function 1  | Functionroom | NULL   |
    +----+-------------+--------------+--------+

    code

    $sql = "SELECT SUM(status='Pending')as pending
                 , SUM(status='Active') as active
            FROM product";
            
    $result = mysqli_query($con, $sql);
    $row = mysqli_fetch_assoc($result);
    
    echo "Pending : <input type=\"button\" class=\"button\" value=\"{$row['pending']}\">
    Active : <input type=\"button\" class=\"button\" value=\"{$row['active']}\">
    ";

    output

    image.png.35977fa47efb7bb8cd0394b10086c43c.png

×
×
  • 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.