Plugin Directory

Changeset 914131


Ignore:
Timestamp:
05/14/2014 07:59:04 PM (12 years ago)
Author:
redeyedmonster
Message:

FIX Some PHP/MySQL platforms couldn't identify primary key

Location:
edit-any-table/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • edit-any-table/trunk/EditAnyTable.php

    r847919 r914131  
    44Plugin URI: http://redeyedmonster.co.uk/edit-any-table/
    55Description: Dashboard widget which allows the editing of all tables in any database
    6 Version: 2.0.0
     6Version: 2.0.1
    77Author: Nigel Bachmann
    88Text Domain: EditAnyTable
     
    3131function EditAnyTable_init()
    3232{
    33     //$plugin_dir = plugin_dir_url(__FILE__);//$plugin_dir.'languages'
    3433    load_plugin_textdomain( 'EditAnyTable', false, basename(dirname(__FILE__)).'/languages/');
    3534}
     
    5756    }
    5857       
    59     //$result = $eat_db->get_col($eat_db->prepare("show tables",null));
    60    
    6158    ?>
    6259   
     
    195192        $eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
    196193       
    197         //Get a single record for column info
    198         $sql = $eat_db->prepare("select * from ".$table2Edit." LIMIT 1",null);
    199         //echo $sql."<br />";
    200         $records = $eat_db->get_results($sql,'ARRAY_N');
    201        
    202         //get column information
    203         $cols = $eat_db->get_col_info('name',-1);
    204         $numeric = $eat_db->get_col_info('numeric',-1);
    205        
     194        $cols = $eat_db->get_results("show columns from ".$table2Edit);
     195
    206196        //build where
    207197        $where = "";
     
    209199        for($i = 0;$i < count($keysArray); $i++)
    210200        {
    211        
    212             //need to find out if the value is for a numeric field or not
    213201            $isNumeric = 0;
    214             for($in = 0; $in < count($cols); $in++)
    215             {
    216                 if($cols[$in] == $keysArray[$i])
    217                 {
    218                     $isNumeric = $numeric[$in] == 1;
    219                 }
    220             }
     202            foreach($cols as $col)
     203            {
     204                if($col->Field == $keysArray[$i])
     205                {
     206                    $isNumeric =
     207                        strpos($col->Type,"int") !== false ||
     208                        strpos($col->Type,"decimal") !== false ||
     209                        strpos($col->Type,"float") !== false ||
     210                        strpos($col->Type,"double") !== false ||
     211                        strpos($col->Type,"real") !== false ||
     212                        strpos($col->Type,"bit") !== false ||
     213                        strpos($col->Type,"boolean") !== false ||
     214                        strpos($col->Type,"serial") !== false ;
     215                }
     216            }
    221217       
    222218            if($keysArray[$i] != "")
     
    239235            }
    240236        }
    241         //echo $where;
    242        
     237
    243238        //prepare the delete statement
    244239        $sql = $eat_db->prepare("DELETE from ".$table2Edit." where ".$where, $vals);
    245         //echo $sql;
    246240        $result = $eat_db->query($sql);
    247241        if($result)
     
    342336    $keysArray = explode("~", $keys);
    343337    $valsArray = explode("~", $values);
     338
    344339    //Connect to the database
    345340    $options = get_option('eat_options');
    346341    $eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
    347342   
    348     //Get a single record for column info
    349     $sql = $eat_db->prepare("select * from ".$table2Edit." LIMIT 1",null);
    350     //echo $sql."<br />";
    351     $records = $eat_db->get_results($sql,'ARRAY_N');
    352    
    353     //get column information
    354     $cols = $eat_db->get_col_info('name',-1);
    355     $types = $eat_db->get_col_info('type',-1);
    356     $primary = $eat_db->get_col_info('primary_key',-1);
    357     $numeric = $eat_db->get_col_info('numeric',-1);
     343    //Get column information
     344    $cols = $eat_db->get_results("show columns from ".$table2Edit);
    358345       
    359346    //build where
     
    365352        //need to find out if the value is for a numeric field or not
    366353        $isNumeric = 0;
    367         for($in = 0; $in < count($cols); $in++)
    368         {
    369             if($cols[$in] == $keysArray[$i])
    370             {
    371                 $isNumeric = $numeric[$in] == 1;
     354        foreach($cols as $col)
     355        {
     356            if($col->Field == $keysArray[$i])
     357            {
     358                $isNumeric =
     359                    strpos($col->Type,"int") !== false ||
     360                    strpos($col->Type,"decimal") !== false ||
     361                    strpos($col->Type,"float") !== false ||
     362                    strpos($col->Type,"double") !== false ||
     363                    strpos($col->Type,"real") !== false ||
     364                    strpos($col->Type,"bit") !== false ||
     365                    strpos($col->Type,"boolean") !== false ||
     366                    strpos($col->Type,"serial") !== false ;
    372367            }
    373368        }
     
    412407        $sql = $eat_db->prepare("select * from ".$table2Edit." LIMIT ".$offSet.", ".$eat_cols."",null);
    413408    }
    414     $records = $eat_db->get_results($sql,'ARRAY_N');
     409
     410    $records = $eat_db->get_results($sql,'ARRAY_A');
    415411   
    416412    //lets work out how many columns we're going to display (max from options)
     
    433429    if($numCols > 0)
    434430    {
    435            
     431        $primaryKeyExists = false;
    436432        ?>
    437433        <div style="overflow: auto">
     
    453449                <?php
    454450            //need to write the results vertically
    455             for($i = 0; $i < count($cols); $i++)
     451            foreach($cols as $col)
    456452            {
    457453                ?>
    458454                <tr>
    459                     <td><?php echo $cols[$i]; ?></td>
     455                    <td><?php echo $col->Field; ?></td>
    460456                <?php
    461457               
     
    463459                {
    464460                    $row = $records[$in];
    465                     if($primary[$i] == 1)
     461                    if($col->Key == "PRI")
    466462                    {
     463                        $primaryKeyExists=true;
    467464                        ?>
    468                         <td style="background-color:white" id="PRIMARY:<?php echo $cols[$i]; ?>"><?php echo $row[$i]; ?></td>
     465                        <td style="background-color:white" id="PRIMARY:<?php echo $col->Field; ?>"><?php echo $row[$col->Field]; ?></td>
    469466                        <?php
    470467                    }
     
    472469                    {
    473470                        ?>
    474                         <td id="<?php echo $cols[$i]; ?>"><input type="text"  value="<?php echo sanitize_text_field($row[$i]); ?>" /></td>
     471                        <td id="<?php echo $col->Field; ?>"><input type="text"  value="<?php echo sanitize_text_field($row[$col->Field]); ?>" /></td>
    475472                        <?php
    476473                    }
     
    486483                for($i = 0; $i < $numCols; $i++)
    487484                {
    488                     if(in_array(1,$primary)) //Do not show save or delete buttons if there is no primary key
     485                    if($primaryKeyExists) //Do not show save or delete buttons if there is no primary key
    489486                    {
    490487
     
    532529function TableDetails()
    533530{
    534     //get options
    535     $options = get_option('eat_options');
    536 
    537     //Get required values
     531    //Get required values
    538532    $table2Edit = $_POST['table2Edit'];
    539533    $eat_cols = $_POST['eat_cols'];
     
    543537    $eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
    544538       
    545     //get a sample row
    546     $result = $eat_db->get_results("select * from ".$table2Edit." LIMIT 0, 1");
    547    
    548     //get column information
    549     $cols = $eat_db->get_col_info('name',-1);
    550     $types = $eat_db->get_col_info('type',-1);
    551     $primary = $eat_db->get_col_info('primary_key',-1);
    552    
    553    
    554     //build the table
    555     //if($eat_db->num_rows > 0) Removed for 1.3.0
    556     //{
    557         ?>
    558         <hr>
    559         <div>
    560             <button class="button-primary" title="Find records matching the values entered" id="buttonFind"><?php _e('Find','EditAnyTable'); ?></button>
    561             &nbsp;
    562             <input type="checkbox" id="fuzzy" />&nbsp;<?php _e('Fuzzy','EditAnyTable'); ?>
    563             <?php
    564             // Check that editor has rights to add
    565             if(current_user_can('administrator') || (current_user_can('editor') && $options['eat_editorPrivAdd']=='yes'))
     539    // Get column info
     540    $cols = $eat_db->get_results("show columns from ".$table2Edit)
     541   
     542   
     543    ?>
     544    <hr>
     545    <div>
     546        <button class="button-primary" title="Find records matching the values entered" id="buttonFind"><?php _e('Find','EditAnyTable'); ?></button>
     547        &nbsp;
     548        <input type="checkbox" id="fuzzy" />&nbsp;<?php _e('Fuzzy','EditAnyTable'); ?>
     549        <?php
     550        // Check that editor has rights to add
     551        if(current_user_can('administrator') || (current_user_can('editor') && $options['eat_editorPrivAdd']=='yes'))
     552        {
     553        ?>
     554        &nbsp;
     555        <button class="button-primary" title="<?php _e('Add a new record with the values entered','EditAnyTable');?>" id="buttonAdd"><?php _e('Add','EditAnyTable'); ?></button>
     556        <?php
     557        }
     558        ?>
     559        &nbsp;
     560        <button class="button" title="<?php _e('Clear all the values','EditAnyTable');?>" id="buttonReset"><?php _e('Reset','EditAnyTable');?></button>
     561    </div>
     562    <hr>
     563    <div style="overflow: auto">
     564        <table id="tableCols">
     565            <tr>
     566                <td><strong><?php _e('Column','EditAnyTable');?></strong></td>
     567                <td><strong><?php _e('Value','EditAnyTable');?></strong></td>
     568            </tr>
     569        <?php
     570            foreach($cols as $col)
    566571            {
    567572            ?>
    568             &nbsp;
    569             <button class="button-primary" title="<?php _e('Add a new record with the values entered','EditAnyTable');?>" id="buttonAdd"><?php _e('Add','EditAnyTable'); ?></button>
     573                <tr>
     574                    <td>
     575                        <?php
     576                            echo $col->Field." (".$col->Type.")";
     577                            if($col->Key=="PRI")
     578                            {
     579                                echo " [PRI]";
     580                            }
     581                        ?>
     582
     583                    </td>
     584                    <td>
     585                        <input type="text" id="<?php echo sanitize_text_field($col->Field); ?>" />
     586                    </td>
     587
     588                </tr>
    570589            <?php
    571590            }
    572591            ?>
    573             &nbsp;
    574             <button class="button" title="<?php _e('Clear all the values','EditAnyTable');?>" id="buttonReset"><?php _e('Reset','EditAnyTable');?></button>
    575         </div>
    576         <hr>
    577         <div style="overflow: auto">
    578             <table id="tableCols">
    579                 <tr>
    580                     <td><strong><?php _e('Column','EditAnyTable');?></strong></td>
    581                     <td><strong><?php _e('Value','EditAnyTable');?></strong></td>
    582                 </tr>
    583             <?php
    584                 for($i=0;$i<count($cols);$i++)
    585                 {
    586                 ?>
    587                     <tr>
    588                         <td>
    589                             <?php
    590                                 echo $cols[$i]." (".$types[$i].")";
    591                                 if($primary[$i]==1)
    592                                 {
    593                                     echo " [PRIMARY]";
    594                                 }
    595                             ?>
    596                            
    597                         </td>
    598                         <td>
    599                             <input type="text" id="<?php echo sanitize_text_field($cols[$i]); ?>" />
    600                         </td>
    601                        
    602                     </tr>
    603                 <?php
    604                 }
    605                 ?>
    606             </table>
    607         </div>
    608         <?php
    609     //}
     592        </table>
     593    </div>
     594    <?php
     595
    610596
    611597    die();
  • edit-any-table/trunk/readme.txt

    r911659 r914131  
    8989== Changelog ==
    9090
     91= 2.0.1 =
     92* FIX Due to a change in the wpdb class in WordPress 3.9 Edit Any Table was unable to identify primary keys on some MySQL/PHP platforms - this has now been rectified.
     93
    9194= 2.0.0 =
    9295* Localization Added
     
    124127
    125128== Upgrade Notice ==
     129
     130= 2.0.1 =
     131* FIX Due to a change in the wpdb class in WordPress 3.9 Edit Any Table was unable to identify primary keys on some MySQL/PHP platforms - this has now been rectified.
    126132
    127133= 2.0.0 =
Note: See TracChangeset for help on using the changeset viewer.