Using application/x-www-form-urlencoded to post

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
elroy
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Oct 22, 2018 8:33 am

Mon Oct 22, 2018 8:45 am

Hi All,

I'm a fair programmer, but I'm just picking up PHP. I'm using another program that makes HTTP posts to a PHP program. I'm using the "application/x-www-form-urlencoded" protocol for these posts.

Here's my question. When I get over to PHP, do I need to wrap my variables in the urldecode function? Or will they already be decoded?

For instance, here's a (mock) piece of code I'm using. You can see the urldecode functions in it. Are those needed? Just as an FYI, similar code is actually working. I'm just trying to understand this stuff.

Code: Select all

<?php
    $dbusername = 'xxxxxxxxxx';
    $dbpassword = 'xxxxxxxxxx';
    $database   = 'xxxxxxxxxx';
    $dbhost     = '10.0.0.102';
    //
    $connection = mysqli_connect("$dbhost", "$dbusername", "$dbpassword", "$database");
    //
    if (mysqli_connect_errno())
    {
        echo "Failed to connect: " . mysqli_connect_error() . "\n";
    }
    else
    {
        if (!empty($_POST))
        {
            $field1           = str_replace("'", "''", urldecode($_POST['field1']));
            $field2           = str_replace("'", "''", urldecode($_POST['field2']));
            //
            $sql =  "INSERT INTO `xxxxxx` (`field1`,  `field2`) VALUES ('$field1', '$field2')";
            //
            $result = mysqli_query($connection, $sql);
            //
            if ($result)
            {
                echo "Fields added.\n";
            }
            else
            {
                echo "Error, issue with adding.\n";
            }
        } 
        else 
        {
            echo "A POST was not received.\n";
        }
    }
    mysqli_close($connection);
?>
Thanks In Advance,
Elroy

Post Reply