How to update a database entry with a wordpress plugin?

admin2025-04-20  0

I created a plugin which creates a database-table (wp_mediopay) and a widget in the admin dashboard. In this widget is a form, which should update an entry in the database table after being submitted.

I simply made it POST to saveentry.php. Then I created saveentry.php in the wp_admin folder:

$newaddress = $_POST["address"];
$table_name = "wp_mediopay";
global $wpdb;
$newaddress = array( 'address' => '$newaddress' );
$data_where = array( 'id' => 1);
$wpdb->update($table_name,$newaddress,$data_where);

but nothing happens. echo $newaddress works.

Weirdly, when I use this:

$myrows = $wpdb->get_results( "SELECT address FROM wp_mediopay WHERE id = 1" )
var_dump($myrows);

I get an error 500. When I do this in my plugin file it works fine.

I created a plugin which creates a database-table (wp_mediopay) and a widget in the admin dashboard. In this widget is a form, which should update an entry in the database table after being submitted.

I simply made it POST to saveentry.php. Then I created saveentry.php in the wp_admin folder:

$newaddress = $_POST["address"];
$table_name = "wp_mediopay";
global $wpdb;
$newaddress = array( 'address' => '$newaddress' );
$data_where = array( 'id' => 1);
$wpdb->update($table_name,$newaddress,$data_where);

but nothing happens. echo $newaddress works.

Weirdly, when I use this:

$myrows = $wpdb->get_results( "SELECT address FROM wp_mediopay WHERE id = 1" )
var_dump($myrows);

I get an error 500. When I do this in my plugin file it works fine.

Share Improve this question asked Sep 30, 2019 at 20:00 Christoph BergmannChristoph Bergmann 1011 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0
$newaddress = array( 'address' => '$newaddress' ); // Incorrect

Is not the same as:

$newaddress = array( 'address' => $newaddress ); // Correct

And you have a missing ; at the end of:

$myrows = $wpdb->get_results( "SELECT address FROM wp_mediopay WHERE id = 1" )

And before the above line you must also have:

global $wpdb;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745117041a285923.html

最新回复(0)