Help with submitting a form to MySQL DB

I have a form with about 7 fields that needs to save to a database. I have PHP validation and the Honeypot capture and the form seems to submit and validate but does not write to the database. I am guessing then the part I need help with is the section where it preps and sends the data to the DB. It is able to connect to the DB.

The error message I get is this: Fatal error: Call to undefined function PrepSQL()

I built this so far with a bunch of tutorials and pieced it together so if it can be improved, let me know. Thanks!


<?php 
//If the form is submitted
if(isset($_POST['submitted'])) {

    //Honeypot
    if(trim($_POST['checking']) !== '') {
        $captchaError = true;
    } else {

        // First Name
        if(trim($_POST['first_name']) === '') {
            $fnameError = 'Please enter your first name.';
            $hasError = true;
        } else {
            $first_name = trim($_POST['first_name']);
        }

        // Last Name
        if(trim($_POST['last_name']) === '') {
            $lnameError = 'Please enter your last name.';
            $hasError = true;
        } else {
            $last_name = trim($_POST['last_name']);
        }

        // Company
        if(trim($_POST['company']) === '') {
            $companyError = 'Please enter your company name.';
            $hasError = true;
        } else {
            $company = trim($_POST['company']);
        }
            
        //Check to make sure sure that a valid email address is submitted
        if(trim($_POST['email']) === '')  {
            $emailError = 'Please enter your email address.';
            $hasError = true;
        } else if (!preg_match("/^[_\\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
            $emailError = 'Please check the email address you entered.';
            $hasError = true;
        } else {
            $email = trim($_POST['email']);
        }

        // If there are no errors, great! Write to the database.
        if(!isset($hasError)) {
            // Connect
            $db = mysql_connect("localhost","username","password");
            if(!$db) die("Error connecting to MySQL database.");
            mysql_select_db("formdb" ,$db);

            // Prep
            $sql = "INSERT INTO contact_details (FIRST_NAME, LAST_NAME, COMPANY, EMAIL, STATE, INTEREST, HEARABOUT)
            VALUES (".
            PrepSQL($first_name) . ", " .
            PrepSQL($last_name) . ", " .
            PrepSQL($company) . ", " .
            PrepSQL($email) . ", " .
            PrepSQL($state) . ", " .
            PrepSQL($interest) . ", " .
            PrepSQL($hear_about) . ")";

            function PrepSQL($value)
            {
                // Stripslashes
                if(get_magic_quotes_gpc())
            {
                $value = stripslashes($value);
            }
            // Quote
            $value = "'" . mysql_real_escape_string($value) . "'";
            return($value);
            }

            // Run SQL
            mysql_query($sql);

            // Set it to saved
            $formsaved = true;
        }
    }// End the Honeypot else wrap
} 
?>

And here is the HTML for the form - just so we have the complete file.


<?php if(isset($formsaved) && $formsaved == true) { ?>
    <div class="thanks">
        <h1>Thanks, <?=$first_name;?></h1>
        <p>Your message is probably popping up on my iPhone right now. I'll get back to you soon!</p>
    </div>

<?php } else { ?>

<?php if(isset($hasError) || isset($captchaError)) { ?>
    <p class="error">There was an error submitting the form.<p>
<?php } ?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <ul>
        <li>
            <label for="first_name">First Name</label>
            <input type="text" id="first_name" name="first_name" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name'];?>" autofocus>
            <?php if($fnameError != '') { ?>
                <label class="error"><?=$fnameError;?></label> 
            <?php } ?>
        </li>
        <li>
            <label for="last_name">Last Name</label>
            <input type="text" id="last_name" name="last_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>">
            <?php if($lnameError != '') { ?>
                <label class="error"><?=$lnameError;?></label>
            <?php } ?>
        </li>
        <li>
            <label for="company">Company</label>
            <input type="text" id="company" name="company" value="<?php if(isset($_POST['company'])) echo $_POST['company']; ?>">
            <?php if($companyError != '') { ?>
                <label class="error"><?=$companyError;?></label>
            <?php } ?>
        </li>
        <li>
            <label for="email">Email</label>
            <input type="text" id="email" name="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>">
            <?php if($emailError != '') { ?>
                <label class="error"><?=$emailError;?></label>
            <?php } ?>
        </li>
        <li>
            <label for="state">State</label>
            <select id="state" name="state">
                <option select="selected">-- Select --</option>
                <option value="Alaska">Alaska</option>
                <option value="Alabama">Alabama</option>
                <option value="Arkansas">Arkansas</option>
                <option value="Arizona">Arizona</option>
                <option value="California">California</option>
                <option value="Colorado">Colorado</option>
                <option value="Connecticut">Connecticut</option>
                <option value="District of Columbia">District of Columbia</option>
                <option value="Delaware">Delaware</option>
                <option value="Florida">Florida</option>
                <option value="Georgia">Georgia</option>
                <option value="Hawaii">Hawaii</option>
                <option value="Iowa">Iowa</option>
                <option value="Idaho">Idaho</option>
                <option value="Illinois">Illinois</option>
                <option value="Indiana">Indiana</option>
                <option value="Kansas">Kansas</option>
                <option value="Kentucky">Kentucky</option>
                <option value="Louisiana">Louisiana</option>
                <option value="Massachusetts">Massachusetts</option>
                <option value="Maryland">Maryland</option>
                <option value="Maine">Maine</option>
                <option value="Michigan">Michigan</option>
                <option value="Minnesota">Minnesota</option>
                <option value="Missouri">Missouri</option>
                <option value="Mississippi">Mississippi</option>
                <option value="Montana">Montana</option>
                <option value="North Carolina">North Carolina</option>
                <option value="North Dakota">North Dakota</option>
                <option value="Nebraska<">Nebraska</option>
                <option value="New Hampshire">New Hampshire</option>
                <option value="New Jersey">New Jersey</option>
                <option value="New Mexico">New Mexico</option>
                <option value=">Nevada">Nevada</option>
                <option value="New York">New York</option>
                <option value="Ohio">Ohio</option>
                <option value="Oklahoma">Oklahoma</option>
                <option value="Oregon">Oregon</option>
                <option value="Pennsylvania">Pennsylvania</option>
                <option value="Puerto Rico">Puerto Rico</option>
                <option value="Rhode Island">Rhode Island</option>
                <option value="South Carolina">South Carolina</option>
                <option value="South Dakota">South Dakota</option>
                <option value="Tennessee">Tennessee</option>
                <option value="Texas">Texas</option>
                <option value="Utah">Utah</option>
                <option value="Virginia">Virginia</option>
                <option value="Vermont">Vermont</option>
                <option value="Washington">Washington</option>
                <option value="Wisconsin">Wisconsin</option>
                <option value="West Virginia">West Virginia</option>
                <option value="Wyoming">Wyoming</option>
            </select>
        </li>
        <li>
            <label for="interest">Interested In</label>
            <select id="interest" name="interest">
                <option select="selected">-- Select --</option>
                <option value="A Demo">A Demo</option>
                <option value="Referral Partnership">Referral Partnership</option>
                <option value="Career Opportunities">Career Opportunities</option>
                <option value="Other">Other</option>
            </select>
        </li>
        <li>
            <label for="hear_about">How did you hear about us?</label>
            <select id="hear_about" name="hear_about">
                <option select="selected">-- Select --</option>
                <option value="Advertisement">Advertisement</option>
                <option value="Internet">Internet</option>
                <option value="Email">Email</option>
                <option value="Referral">Referral</option>
                <option value="Facebook/Twitter/LinkedIn">Facebook/Twitter/LinkedIn</option>
                <option value="Newsletter">Newsletter</option>
                <option value="Other">Other</option>
            </select>
        </li>
    </ul>
    <input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit"  class="buttons">Send</button>
    <label for="checking" class="screenReader">Do not fill in this field</label>
    <input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" />
</form>
<?php } ?>

Put the function at the start of the PHP code.
You can’t call a function that hasn’t been defined yet.

Yes… that was it. Thanks! Taking the time to learn some basic PHP and MySQL!

That’s great! Don’t hesitate to ask if you run into something you can’t solve/find the answer to :wink:

Off Topic:

I just saw the ‘Proud new dad!’ title. Congratulations! Or am I late and have you been a dad for a while already?