Using a HTML form and PHP to access an external database on Wordpress

Viewed 28

I'll start by explaining what I'm trying to achieve and then where I'm at.

I want to have a form on my website that allows any user to verify information. The form has 2 fields, the first being an 8 digit number and the second being a drop down with more numbers. When the form is submitted it will send the query to an external database and check to see if there's a match for a single row with both pieces of data present. It will then return the data that was present in that row within the webpage or return nothing if there was no match.

I can get this to work using XAMPP and doing it locally but I cannot get it to work on WordPress.

This is the PHP code.

//Entered Details - Variables
$enterednumber1 = $_POST['number1'];
$enterednumber2 = $_POST['number2'];
$query = "SELECT * FROM dbo.FirstDB WHERE NumberOne = $enterednumber1 AND NumberTwo = $enterednumber2 ";

//Check Results - Nothing Shown If No Data
if (($result->sqlsrv_query($con,$query)) && (sqlsrv_num_rows($result) > 0)) {
    while ($row = sqlsrv_fetch_array($result)) {
        if ($row[5] > 0) {
            print("<h3>Number: </h3>".$row[5]."\n");
        }
        if ($row[6] > 0) {
            print("<h3>Number: </h3>".$row[6]."\n");
        }
        if ($row[7] > 0) {
            print("<h3>Number: </h3>".$row[7]."\n");
        }
    }
} else {
    echo "No Results";
}
sqlsrv_close($con);     

The HTML looks like this

<form class="verify" name="verify" method="POST" action="verify.php">
            <input type="text" autocomplete="off" placeholder="Number" name="number1" pattern="^[0-9]{8}$" required="" maxlength="8">
            <select class="number2" name="number2" required="">
                    <option value="10">10</option>
                    <option value="8">8</option>
                    <option value="6">6</option>
                    <option value="4">4</option>
                    <option value="3">3</option>
                    <option value="2">2</option>
                    <option value="1">1</option>
                    <option value="0">0</option>
            </select>
            </br>
            <input type="submit" value="Submit">
            </form>   

I've tried adding the HTML to a webpage on Wordpress using a plugin and then adding the verify.php file to the theme folder but I get a 404 anytime the form is submitted. Is there something wrong with the code or did I upload the file to the wrong spot? Also some patience would be greatly appreciated as all this is 100% self taught. I've been trying to figure this out as I'm going along.

1 Answers

You could place verify.php at root level, and use action="/verify.php". If you don't want to get into WordPress (wp) dev or don't need wp functionalities, that would be enough. Just redirect afterwards to a wp page.

Note: You should also fix your SQL injection vulnerabilities. Also, this form is vulnerable to CSRF attack. Don't get discouraged those are experience thing.

Personally I would use function.php (if I was developing the theme) and insert code like this:

if (is_post()) {

    if ($_POST["action"]=="my-identifying-value-for-my-form") {
        // your logic
      
    }
}
Related