Jump to content

Proper steps for testing my mariadb connection


LivingLearning
 Share

Recommended Posts

Hi guys,

Am still new at this, so be patient with me.

I have my local xampp test server all setup and appears to be working fine.

Am ready to test connection from my html form - PHP (PDO) - mariadb

I'm getting a bit lost in all the manuals for the proper placement and setup.

Am using win8.1 and current recommended ver of xampp.  Latest release.

Briefly:  I have a simple html form set up (to test off of one simple survey q).  I'll post all code below.

<form> has --- action = "GetFormData.php" method = "post" ---set

Submit button set as follows in html file under <form>:

                        <label>Press 'Submit' when you are done.</label>
                        <input type="submit" value="Submit" name="submit_button">


Basically, i'm confused on the exact steps/(.php) files I need to use in order to get the form data (and validate it), then setup and test a mariadb connection, then to pass that data to the database.  Is it all done in one .php file on the server side, or should I be having multiple files for each step?

I understand that the .php file that works with the <form> data should be on server side, so i have it under my 'htdocs' folder in test server (localhost).

I keep checking in the faq and other W3Schools tutorial areas, but am missing the bigger picture on the proper setup somewhere and what goes where.

And, what is best way to manually test the .db connection.  From a command line somehow or through some control panel?

--------------------  my scripts are as follows so far:

Main pg html file containing <form> i'm testing: (only pieces in question)

<head>
    <meta charset="UTF-8">
    <title>SciFi Monsters</title>
    <link href="Framework-MonstersOfSciFi.css" type="text/css" rel="stylesheet">
    <link href="style-MainPg.css" type="text/css" rel="stylesheet">

    <link href="/localhost/GetFormData.php" type="text/php" rel="stylesheet">
    <link href="/localhost/testConnect.php" type="text/php" rel="stylesheet">
</head>

(further down, here is the survey part (part i'm testing only):

        <div id="survey-div">
            <form id="survey-form" class="MainPgTxtBckgColor" action = "GetFormData.php" method = "post">
                <fieldset id="survey-fieldset">
                    <legend>Take one of our quick surveys.</legend>
                    <p>
                        <label>1. Who is your <strong>favorite monster</strong> character (Godzilla related or not):</label>
                        <input id="FavMonster" type = "text" />
                    </p>
(there are only 5 survey q's, so not posting all.  Only testing one)

That's it for the main html file.  (I only have one form for this site, just building and playing with things to learn first.)

--------------------------------------------------

Now for the .php files:

First:  The 'GetFormData.php file  (under localhost/htdocs)

This file is setup, from what I've read about so far, to get the form data and sanitize it.  (not sure if I should have something else in here, or if the .db connection .php file should go in here as well.)

<!DOCTYPE html>
<html lang = "en-US">
    
<head>
    <meta charset = "UTF-8">
    <title>GetFormData.php</title>
</head>
    
<body>
    <p>
    <?php
    // define variables and set to empty values
    $favmonsterErr = "";
    $favmonster = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["favmonster"])) {
            $favmonsterErr = "Favorite Monster is required";
        } else {
            $favmonster = test_input($_POST["favmonster"]);

    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$favmonster)) {
      $favmonsterErr = "Only letters and white space allowed"; 
    }
    }
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
    </p>
</body>
</html>

----------------------------------------  

Now the testConnect.php file:

This file is setup to make a test connection to the .db.  After this, should i have another .php script, which would then try to post the data to the .db, or does it all happen in one .php file.

<!DOCTYPE html>
<html lang = "en-US">
    
    <head>
        <meta charset = "UTF-8">
        <title>testConnect.php</title>
    </head>
    
    <body>
        <p>
            <?php
            $servername = "localhost";
            $username = "Any";
            $password = "McG1v3r-9";
              try {
                $conn = new PDO("mysql:host=$servername;dbname=test-survey1", $username, $password);
            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected successfully"; 
            }
        catch(PDOException $e)
            {
            echo "Connection failed: " . $e->getMessage();
            }
        ?>
        </p>
    </body>
</html>

---------------------

This is all I have for now.  Getting a bit confused on the proper setup of stuff.

Thanks in advance,

 

Link to comment
Share on other sites

That's how you connect and display an error if it happened.

After this, should i have another .php script, which would then try to post the data to the .db, or does it all happen in one .php file.

Whatever you want to happen with the data from the form happens in the file where the form submits to.  So that file would connect to the database (or include another file which does), and then process the form data however you want to.  It's common to have things like database connections and other configuration code in a single file which any other file can include.  That way there's only one place if you ever need to change any of that stuff.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...