这是自定义模板。我编写crud时使用wordpress查询发送数据库自定义表中的数据并从数据库中获取数据,插入查询工作,但更新和删除查询不工作。下面我给出更新和删除查询,请建议是否有人知道?
数据已插入,但刷新时,数据会自行删除,而不会更新或删除这是插入查询
<?php
/*
* Template Name:Form Template page
*/
get_header(); ?>
<?php
global $wpdb;
$table= \'wp_contact_form\';
if (isset($_POST[\'submit\'])) {
$data = array(
\'names\' => $_POST[\'yourname\'],
\'emails\' => $_POST[\'customer_email\'],
\'gender\' => $_POST[\'customer_gender\'],
\'age\' => $_POST[\'customer_age\']
);
$success=$wpdb->insert( $table, $data );
if($success){
echo \'data has been save\' ;
}
else {
echo "data not save";
}
}
?>
<div class="container">
<div class="row ">
<div class="col-md-12 pb-5">
<form method="post">
Your name:<input type="text" name="yourname" id="yourname">
Your Email:<input type="text" id="customer_email" name="customer_email"/><br>
Gender:<input type="radio" name="customer_gender" value="male">Male
<input type="radio" name="customer_gender" value="female">Female<br>
Your Age:<input type="text" name="customer_age" id="customer_age"><br>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="show" value="Show"> <a href="http://localhost/vishal_nov/edit-form/">show</a>
</form>
</div>
</div>
</div>
<?php
get_footer();
?>
这是更新和删除查询
<?php
/*
* Template Name: Show Form Template Page
*/
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="POST">
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Gender</th>
<th>Age</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
global $wpdb;
$table= \'wp_contact_form\';
$result = $wpdb->get_results ( "SELECT * FROM $table" );
if(!empty($result)){
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->id;?></td>
<td><?php echo $print->names;?></td>
<td><?php echo $print->emails;?></td>
<td><?php echo $print->gender;?></td>
<td><?php echo $print->age;?></td>
<td><button class="" name="update">Edit</button></td>
<td><a href="#" id="\'.$print->id.\'" class="del_btn">Delete</a></td>
</tr>
<?php
}
}
?>
</table>
</form>
<?php
global $wpdb;
$id= $print->id;
$table= \'wp_contact_form\';
if (isset($_POST[\'update\'])) {
$data =array(
\'names\' => $_POST[\'yourname\'],
\'emails\' => $_POST[\'customer_email\'],
\'gender\' => $_POST[\'customer_gender\'],
\'age\' => $_POST[\'customer_age\']
);
$wherecondition=array(
\'id\'=>$id
);
$updated=$wpdb->update($table, $data, $wherecondition);
}
?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(\'.del_btn\').click(function(){
var el = this;
var deleteid = $(this).attr(\'id\');
var confirmalert = confirm("Are you sure?");
if (confirmalert == true) {
// AJAX Request
$.ajax({
url: \'delete1.php\',
type: \'POST\',
data: { dell_id:deleteid },
success: function(response){
if(response == 1){
$(el).closest(\'tr\').css(\'background\',\'tomato\');
$(el).closest(\'tr\').fadeOut(800,function(){
$(this).remove();
});
}
else{
alert(\'Invalid ID.\');
}
}
});
}
});
});
</script>
</div>
</div>
在delete中定义delete查询。php文件,但显示未找到的页面
<?php
global $wpdb;
$table= \'wp_contact_form\';
$id=$_POST[\'dell_id\'];
$result = $wpdb->delete($table, array(\'id\' => $id));
if(!empty($result))
{
if($result){
echo "success";
}
}
?>
这是屏幕截图
https://prnt.sc/wbgzgt以下是控制台中未找到ajax jquery错误页面的屏幕截图
https://prnt.sc/wc1l4s