this is custom template. I wrote crud by using wordpress query to sent data in custom table of database and get from database, insert query worked, but update and delete query not working. Below I give update and delete query, please suggest if anyone knows?
data inserted but when refresh, data deleted by itself and not update or delete this is insert query
<?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();
?>
this is update and delete query
<?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 query define in delete.php file but it shows page not found
<?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";
}
}
?>
here is screen shot https://prnt.sc/wbgzgt here is screenshot for error page not found for ajax jquery in console https://prnt.sc/wc1l4s
id
is$result->id
, but$result
is an array of all your rows. It doesn't have an ID. If you want to update a specific row, you need to send the ID as a form field so that you can tell which row needs to be updated.