0

I am using postgreSQL, PHP, and an HTML form. The form is a simple scholarship application. My connection script seems to work fine because when I click submit on the HTML form it echos "connected" and doesn't die, however no data from the form is transferred to my table. Please any guidance.

My PHP connection script: connect.php

        try {
            $dbConn = new PDO('pgsql:host=' . DB_HOST . ';'
                                . 'port=' . DB_PORT . ';'
                                . 'dbname=' . DB_NAME . ';'
                                . 'user=' . DB_USER . ';'
                                . 'password=' . DB_PASS);
            $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set error mode to exception
            echo “Connected”;
        } catch (PDOException $e) {
            $fileName = basename($e->getFile(), ".php"); // File that triggers the exception
            $lineNumber = $e->getLine();          // Line number that triggers the exception
            die("[$fileName][$lineNumber] Database connect failed: " . $e->getMessage() . '<br/>');
}
?> 

My PHP submission script: form.php

<?php
    require 'connect.php';

    $sid = $_POST['sid'];
    $firstName = $_POST['fname'];
    $preferredName = $_POST['pname'];
    $lastName = $_POST['lname'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $inSchool = $_POST['inSchool'];
    $gDate = $_POST['gDate'];
    $gpa = $_POST['gpa'];
    $essay = $_POST['essay'];
    $submit = $_POST['submit'];

 

    if ($sid = ''){
        $query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
        $statement = $dbConn->prepare($query);
        $statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
    }
    else{
        $query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
        $statement = $dbConn->prepare($query);
        $statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
    }
    $query = 'select sid from student where firstname = ? and lastname = ? limit 1';
    $statement = $dbConn->prepare($query);
    $statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
    $results = $statement->fetch();

    echo $results[0];

?>

My HTML Form: Application.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="Style.css">
    <title>Application</title>
</head>
    <body>
        <form action="form.php" method="post">
            <header>
                <img class="logo" src="" alt="logo">
                <div>Asterisks Corporation Scholarship Application</div>
            </header>

            <p class="general">Please fill in all information to apply for the Asterisk's Scholarship.<p>
            First Name:
                <input type="text" size="15" name="fname" required />
            Preferred Name:
                <input type="text" size="15" name="pname" >
            Last Name:
                <input type="text" size="15" name="lname" required />


            <p class="newSection">Contact Information</p>
            Address:
                <input type="text" size="50" name="address" required />
            City:
                <input type="text" size="15" name="city" required />
            State:
                <input type="text" size="15" name="state" required />
            Zip Code:
                <input type="text" size="1" name="zip" placeholder="####" required /><br><br>
            Phone Number:
                <input type="text" size="10" name="phone" placeholder="(###)-###-###">
            Email Address:
                <input type="text" size="50" name="email" required />


            <p class="newSection">Academic Information</p> 
            Are you currently enrolled in school?
                <input type="checkbox" id="Yes" name="yesBox">
                <label for="Yes">Yes</label>
                <input type="checkbox" id="No" name="noBox">
                <label for="No">No</label><br><br>
            What school are you enrolled?
                <input type="text" size="50" name="schoolsEnrolled"><br><br>
            What is/was your date of Graduation?
                <input type="date" name="graduationDate"><br><br>
            GPA
                <input type="text" size="1" name="gpa">


            <p class="newSection">What institutions have you applied?</p> 
            <input type="text" name="" value="" id="school" name="schoolsApplied">
            <button onclick="addToList()" type="button" name="button" id="addButton">Add School</button><br>
            <ul id="schoolList"></ul>

            <p class="newSection">Please write a small essay as to why you should receive this scholarship and what your plans are after graduation.</p>
            <textarea id="essay" style="width: 500px; height: 200px;" alignment="left" onkeyup="wordCounter(),wordsRemaining()" name="essay"></textarea>
            <div>
            300 Words Minimum & 500 Words Maximum 
            <div> Word Count: <span id="wordCount">0</span></div>
            <div> Words Remaining: <span id="wordsRemaining">0</span></div>
            
            </div>


            <p class="newSection">Please confirm each of the following:</p>
            <input type="checkbox" id="Transcript" name="transcriptConfirm" required />
                <label for="Transcript">I have sent in all of my transcripts</label><br>
            <input type="checkbox" id="Schools" name="schoolConfirm" required />
                <label for="Schools">All schools that I am considering are in the US</label><br>
            <input type="checkbox" id="Awards" name="awardConfirm" required />
                <label for="Awards">I understand that the award is $5,000 per year for four years</label><br>
            <input type="checkbox" id="Confirm" name="amountConfirm" required />
                <label for="Confirm">I have received confirmation that my recommenders have emailed their letters to the Scholarship's Coordinator</label><br><br>
            
            Please type your signature in the text box below: <br><br>
                <input type="text" size="20" name="signature" required />

            <div><br>    
            <input type="submit" value ="Submit" name="submit">
            <input type="reset" value="Start Over" onclick="MinMax()">
            </div>

            <script>
                function addToList(){
                let school= document.getElementById("school").value;
                document.getElementById("schoolList").innerHTML += ('<li>'+ school+'</li>');
                };

                function wordCounter(text){
                var count= document.getElementById("wordCount");
                var input= document.getElementById("essay");
                var text=essay.value.split(' ');
                var wordCount = text.length;
                count.innerText=wordCount
                }
                function wordsRemaining(text){
                var count= document.getElementById("wordCount");
                var input= document.getElementById("essay");
                var remaining = document.getElementById("wordsRemaining");
                var text=essay.value.split(' ');
                var wordCount = text.length;
                remaining.innerText=300-wordCount
                }


            </script>
        </form>
    </body>
</html>
ILoveMusic
  • 31
  • 6
  • Does this answer your question? [Why does this PDO statement silently fail?](https://stackoverflow.com/questions/32648371/why-does-this-pdo-statement-silently-fail) – ADyson Nov 04 '22 at 23:27
  • P.S. Some of your PHP code is looking for the wrong items in the $_POST data. e.g. you look for `$_POST['fname']` but in the HTML form you wrote `name="firstname"`. There are other similar mistakes. I suggest going back through the code and double-checking everything, because clearly you can't save that data if those don't match. This should be throwing out some warnings in the PHP about undefined array indexes. If it's not, make sure you've turned PHP error reporting on, and/or log in the PHP error log file (if logging is enabled instead). – ADyson Nov 04 '22 at 23:29
  • You also seem to have quite a few items in your HTML form where no attempt is made in the PHP to capture and save them, and equally fields mentioned in the PHP which don't seem to have an obvious match in the HTML form (even allowing for differences such as I described in my first comment). It's unclear if this is deliberate. But if I had to guess, I'd say you've perhaps taken some PHP code which is intended to process a somewhat different form, and haven't yet spent any time adapting it to process the form shown in your question. – ADyson Nov 04 '22 at 23:31
  • @ADyson I haven't completely added all the PHP code yet, because I just simply wanted to get my foot in the door with just my student table data being updated. My connection script did throw errors and worked fine, and I fixed it so it shows "Connected" now when I submit my form. I fixed the code from your suggestions and the post you linked, but I feel I am missing something. It still isn't up dating my data table "student". – ILoveMusic Nov 05 '22 at 00:04
  • `if ($sid = '')` looks like a typo:`=` is to assign a value, not compare two values. I expect you meant `==`. But since you execute the same insert query regardless of what the outcome of that `if` is, it really shouldn't matter. The way you've got it now, the insert should succeed, or throw an error - there aren't really any other outcomes. Are you sure it's not inserting anything? Are you looking in the right place in your database? Or even looking in the correct database? What tool are you using to check what's inserted? – ADyson Nov 05 '22 at 08:20

0 Answers0