Click here to Skip to main content
15,888,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to update and delete database in wordpress by using wordpress query, but it's not working.I also try ajax for data updating and deleting but it's not working. I provide all code in below section

What I have tried:

this code represents table which I get through database and
update and delete query, if anyone knows what I did wrong, please help me
<?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 ) {   //here $result is an array
													// $print variable is for current row		
 		?>
				<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><input type="submit" value="Edit" name="update"></td>
				<td><input type="submit" value="Delete" name="delete"></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
				    			);
				$wpdb->update($table, $data, $wherecondition);
	// $wpdb->update('table_name', array('id'=>'1', 'name'=>'parvez', array('id' => '2')));


			}	
				

				if (isset($_POST['delete'])) {
					// echo "gg";
					// die;
				    $wpdb::delete($table, $wherecondition);

				    }
				    ?>
Posted
Updated 22-Dec-20 1:51am
v2
Comments
CHill60 22-Dec-20 8:30am    
What do you mean by "not working" - describe what does (or doesn't) happen
vishal@1 23-Dec-20 5:42am    
@CHill60, update query not update the data and delete query not delete the data

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900