Hi i have this worpdress project plugin ive created. The data is successfully rendered in the wp-admin from the database. Now my problem is it seems my sql delete query is not working which is my sql delete code seems to be okay. here is my code below add_action('admin_menu', 'zipcode_menu');
function zipcode_menu(){
add_menu_page( 'Zipcode Page', 'Zipcode', 'manage_options', 'zipcode', 'zipcode' );
}
function zipcode() {
// Now display the settings editing screen
echo '<div class="wrap">';
// header
echo "<h2>" . __( 'Zipcode', 'zip' ) . "</h2>";
// settings form
if(isset($_POST['Submit'])){
global $wpdb;
$zipcode = $_POST['zipcode'];
$wpdb->query("INSERT INTO zipcode(zipcode)VALUES('$zipcode')" );
}
?>
<form method="post" action="">
<table class="form-table">
<tr valign="top">
<td><input type="text" name="zipcode" placeholder="Zipcode: " value=""/></td>
</tr>
</table>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Add Zipcode') ?>" />
</p>
</form>
</div>
<br>
<br>
<?php
global $wpdb;
$sql = "SELECT id, zipcode FROM zipcode;";
$results = $wpdb->get_results($sql);
?>
<script type="text/javascript">
jQuery.noConflict();
function goDelete($id){
var x = confirm('Are you sure you want to delete this?');
if(x){
jQuery.post("/wp-content/themes/doozo/lib/script.php", { id:$id }, function(data){
});
}else{
return false;
}
}
</script>
<table width="600px">
<tr>
<th>
Zipcode
</th>
<th>
Options
</th>
</tr>
<?php foreach($results as $r): ?>
<tr class="zipcode-<?php echo $r->id; ?>">
<th>
<?php echo $r->zipcode; ?>
</th>
<th>
<a onclick="goDelete('<?php echo $r->id; ?>')" href="#">Delete</a>
</th>
</tr>
<?php endforeach; ?>
</table>
<?php
}
Now my script.php code to delete the data from the table
global $wpdb;
$id = $_POST['id'];
echo $id;
// sql to delete a record
$sql = "DELETE FROM zipcode where id=$id";
echo $sql; exit;
Can someone try to help me figured this thing out? It seems my delete query is not working. Any help is muchly appreciated. TIA
Try this:
<?php
global $wpdb;
$id = $_POST['id'];
echo $id;
// sql to delete a record
//$sql = 'DELETE FROM zipcode WHERE id="'. $id .'"';
$wpdb->delete( 'zipcode', array( 'id' => $id ) );
?>