dbh)
{
echo ''.__('Unable to connect to database, check your settings','EditAnyTable').'';
return;
}
?>
:
'.__('Cannot update this record because there are no primary keys in the table','EditAnyTable');
}
else
{
//build where array
$whereArray = array();
for($i = 0;$i < count($keysArray); $i++)
{
if($keysArray[$i] != "")
{
$newParam = array($keysArray[$i] => sanitize_text_field($valsArray[$i]));
$whereArray = array_merge($whereArray,$newParam);
}
}
//build set commands
$setArray = array();
for($i = 0;$i < count($keysUArray); $i++)
{
if($keysUArray[$i] != "")
{
$newParam = array($keysUArray[$i] => sanitize_text_field($valsUArray[$i]));
$setArray = array_merge($setArray,$newParam);
}
}
//Connect to the database
$options = get_option('eat_options');
$eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
if($eat_db->update($table2Edit,$setArray,$whereArray))
{
echo '
'.__('Record Updated','EditAnyTable').'';
}
else
{
echo '
'.__('Unable to update record','EditAnyTable').'
'.__('This is usually because nothing has changed or the record no longer exists.','EditAnyTable');
}
if(current_user_can('administrator') && $options['eat_debug']=='ON')
{
echo '
DEBUG MODE ON
'.$eat_db->last_query;
}
}
die();
}
add_action('wp_ajax_DeleteRecord','DeleteSelected');
function DeleteSelected()
{
//get the posted values
$table2Edit = $_POST['table2Edit'];
$keys = $_POST['keys'];
$values = $_POST['values'];
// get the key/value pairs for the delete
$keysArray = explode("~", $keys);
$valsArray = explode("~", $values);
if(count($keysArray)==0)
{
echo '
'.__('Cannot delete this record because there are no primary keys in the table','EditAnyTable');
}
else
{
//Connect to the database
$options = get_option('eat_options');
$eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
$cols = $eat_db->get_results("show columns from ".$table2Edit);
//build where
$where = "";
$vals = array();
for($i = 0;$i < count($keysArray); $i++)
{
$isNumeric = 0;
foreach($cols as $col)
{
if($col->Field == $keysArray[$i])
{
$isNumeric =
strpos($col->Type,"int") !== false ||
strpos($col->Type,"decimal") !== false ||
strpos($col->Type,"float") !== false ||
strpos($col->Type,"double") !== false ||
strpos($col->Type,"real") !== false ||
strpos($col->Type,"bit") !== false ||
strpos($col->Type,"boolean") !== false ||
strpos($col->Type,"serial") !== false ;
}
}
if($keysArray[$i] != "")
{
if($i != 0)
{
$where = $where." and ";
}
if($isNumeric)
{
$where = $where.$keysArray[$i]." = %d";
}
else
{
$where = $where.$keysArray[$i]." = %s";
}
$vals[] = sanitize_text_field($valsArray[$i]);
}
}
//prepare the delete statement
$sql = $eat_db->prepare("DELETE from ".$table2Edit." where ".$where, $vals);
$result = $eat_db->query($sql);
if($result)
{
echo '
'.__('Record Deleted','EditAnyTable').'';
}
else
{
echo '
'.__('Unable to delete record','EditAnyTable').'
';
$eat_db->show_errors();
$eat_db->print_error();
$eat_db->hide_errors();
}
if(current_user_can('administrator') && $options['eat_debug']=='ON')
{
echo '
DEBUG MODE ON
'.$eat_db->last_query;
}
}
die();
}
add_action('wp_ajax_AddRecord','CreateRecord');
function CreateRecord()
{
//get the posted values
$table2Edit = $_POST['table2Edit'];
$keys = $_POST['keys'];
$values = $_POST['values'];
$eat_cols = $_POST['eat_cols'];
$offSet = "0";
?>
sanitize_text_field($valsArray[$i]));
$insertArray = array_merge($insertArray,$newParam);
}
}
//Connect to the database
$options = get_option('eat_options');
$eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
if($eat_db->insert($table2Edit,$insertArray))
{
echo '
'.__('New Record Created','EditAnyTable');
}
else
{
echo '
'.__('Unable to create new record','EditAnyTable').'
';
$eat_db->show_errors();
$eat_db->print_error();
$eat_db->hide_errors();
}
if(current_user_can('administrator') && $options['eat_debug']=='ON')
{
echo '
DEBUG MODE ON
'.$eat_db->last_query;
}
die();
}
//PHP functions to handle the Ajax requests
add_action('wp_ajax_GetRecords','ReturnRecords');
function ReturnRecords()
{
$table2Edit = $_POST['table2Edit'];
$keys = $_POST['keys'];
$values = $_POST['values'];
$offSet = $_POST['offSet'];
$eat_cols = $_POST['eat_cols'];
$fuzzy = $_POST['fuzzy'];
?>
get_results("show columns from ".$table2Edit);
//build where
$where = "";
$vals = array();
for($i = 0;$i < count($keysArray); $i++)
{
//need to find out if the value is for a numeric field or not
$isNumeric = 0;
foreach($cols as $col)
{
if($col->Field == $keysArray[$i])
{
$isNumeric =
strpos($col->Type,"int") !== false ||
strpos($col->Type,"decimal") !== false ||
strpos($col->Type,"float") !== false ||
strpos($col->Type,"double") !== false ||
strpos($col->Type,"real") !== false ||
strpos($col->Type,"bit") !== false ||
strpos($col->Type,"boolean") !== false ||
strpos($col->Type,"serial") !== false ;
}
}
if($keysArray[$i] != "")
{
if($i != 0)
{
$where = $where." and ";
}
if($isNumeric)
{
$where = $where.$keysArray[$i]." = %d";
$vals[] = sanitize_text_field($valsArray[$i]);
}
else
{
if($fuzzy == "checked")
{
$where = $where.$keysArray[$i]." like %s";
$vals[] = sanitize_text_field('%'.$valsArray[$i].'%');
}
else
{
$where = $where.$keysArray[$i]." = %s";
$vals[] = sanitize_text_field($valsArray[$i]);
}
}
}
}
//Get the records
if(count($vals)>0)
{
$sql = $eat_db->prepare("select * from ".$table2Edit." where ".$where." LIMIT ".$offSet.", ".$eat_cols."",$vals);
}
else
{
$sql = $eat_db->prepare("select * from ".$table2Edit." LIMIT ".$offSet.", ".$eat_cols."",null);
}
$records = stripslashes_deep($eat_db->get_results($sql,'ARRAY_A'));
//lets work out how many columns we're going to display (max from options)
$numCols = $eat_db->num_rows;
?>
0)
{
?>
0)
{
$primaryKeyExists = false;
?>
DEBUG MODE ON
'.$eat_db->last_query;
}
die();
}
add_action('wp_ajax_GetTable','TableDetails');
function TableDetails()
{
//Get required values
$table2Edit = $_POST['table2Edit'];
$eat_cols = $_POST['eat_cols'];
//connect to the database
$options = get_option('eat_options');
$eat_db = new wpdb($options['eat_user'],$options['eat_pwd'],$options['eat_db'],$options['eat_host']);
// Get column info
$cols = $eat_db->get_results("show columns from ".$table2Edit)
?>
Settings';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'your_plugin_settings_link' );
?>