嗨,我已经创建了这个worpdress项目插件。数据在数据库中的wp-admin中成功呈现。现在我的问题是,似乎我的sql删除查询不起作用,这是我的sql删除代码似乎没关系。这是我的代码如下 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
}
现在我的script.php代码从表中删除数据
global $wpdb;
$id = $_POST['id'];
echo $id;
// sql to delete a record
$sql = "DELETE FROM zipcode where id=$id";
echo $sql; exit;
有人可以帮我解决这个问题吗?似乎我的删除查询无法正常工作。任何帮助都非常感谢。 TIA
答案 0 :(得分:0)
试试这个:
<?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 ) );
?>