How to save data from HTML form to database table in WordPress?

I have a WordPress theme and am trying to save data from an HTML form to a database.

I created an HTML form and added a Save and Close button, which calls a JavaScript function with a name saveData()that takes data from the form and sends it to addrow.php, which should save the data in the database table with the name vel.

I think the problem is addrow.phpbecause in WordPress you need to use a global $wpdbor some other thing.

What would be a simple example of how to save data from an HTML form to a database table in a WordPress application?

Code addrow.php:

<?php
    require("phpsqlinfo_dbinfo.php");

    // Gets data from URL parameters
    $nombre = $_GET['nombre'];
    $direccion = $_GET['direccion'];
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $tipo = $_GET['tipo'];

    // Opens a connection to a MySQL server
    $connection = mysql_connect ("localhost", $username, $password);
    if (!$connection) {
        die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
        die ('Can\'t use db : ' . mysql_error());
    }

    // Insert new row with user data
    $query = sprintf("INSERT INTO vel " .
    " (id, nombre, direccion, lat, lng, tipo ) " .
    " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
    mysql_real_escape_string($nombre),
    mysql_real_escape_string($direccion),
    mysql_real_escape_string($lat),
    mysql_real_escape_string($lng),
    mysql_real_escape_string($tipo));

    $result = mysql_query($query);

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
?>
+3
source share
1

; $wpdb. WordPress , .

, , ( , ):

$wpdb->insert( 'table', array( 'column1' => 'value1', 'column2' => 123 ), array( '%s', '%d' ) )

(, addrow.php ?), .

+4

All Articles