- Looking method to get data from wp-mysql
- create form to adjust wp-mysql from admin side
- connect with woocommerce
1. GET DATA FROM MYSQL
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'Custom Plugin', 'custom_dashboard_help');
}
function custom_dashboard_help() {
global $wpdb;
//insert first row as ID so that we can update database later with form
$wpdb->get_row( $wpdb->prepare( "INSERT INTO wp_pluck SET ID=1" ) );
//using iframe to connect with backdoor plugin form
echo '<iframe src="http://localhost/optimize/wp-content/plugins/godek/godek-update.php" title="Embed Inside Admin"></iframe>';
// *optional, for display echo or debug
$pluck = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_pluck WHERE ID = 1" ) );
echo $pluck->number;
}
2. UPDATE WPDB ROW
$wpdb->get_row( $wpdb->prepare( "UPDATE wp_pluck SET number='3' WHERE id=1" ) );
3. IN PLUGIN FOLDER CREATE PHP FILE
FORM FILE
<?php
//create godek-update.php to update
$produk_id=1;
$servername = "localhost";
$username = "root";
$password = "";
$db = "optimize";
$conn = new mysqli($servername, $username, $password, $db);
if($conn->connect_error){
die("Connection failed ".$conn->connect_error);
}
$sql = "select * from wp_pluck where id='$produk_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
$row = $result->fetch_assoc();
$number = $row["number"];
echo
"<html>
<body>
<form action='godek-process.php' method='post'>
Student ID: $produk_id<br>
<input type='hidden' name='produk_id' value='$produk_id'>
Name: <input type='text' name='number' value='$number'><br>
<input type ='submit'>
</form>
</body>
</html>";
} else {
echo "Not Found";
}
$conn->close();
?>
EXECUTE FILE
<?php
//create godek-process.php execute
$produk_id = $_POST["produk_id"];
$number = $_POST["number"];
$servername = "localhost";
$username = "root";
$password = "";
$db = "optimize";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
$sql = "update wp_pluck set number='$number' where ID='$produk_id'";
if ($conn->query($sql) === TRUE) {
echo "Records updated: ID ".$produk_id.":".$number;
} else {
echo "Error: ".$sql."<br>".$conn->error;
}
$conn->close();
?>