mindapolis Posted November 20, 2015 Share Posted November 20, 2015 I am so sorry for all the questions. Once I understand how to get the form data into the database I should not have as many questions. could someone help me understand why I'm getting this error message? Fatal error: Call to a member function prepare() on a non-object in /web/html/mediaservicesunlimited.com/contactTest.php on line 10 Supposedly it 's with my database connection, but i have done tests and I can connect with the database just fine. functions.php //database connection function databaseConnection() { $hostname = "hercules.xymmetrix.net"; $username = "d"; $password = "s"; //the log in information is in the file try { $db = new PDO("mysql:host=$hostname;dbname=www_mediaservicesunlimited_com", $username, $password); } catch(PDOException $e) { echo $e->getMessage(); } } <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Untitled Document</title> <?php require_once('functions.php'); if(isset($_POST['submit'])) { databaseConnection(); $stmt = $mysqli ->prepare("INSERT INTO clients (fname, lname, orgName, address, city, state, zipcode, phone, fax, email, confirmEmail, projectOptions, projectOverview, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt -> bind_param('sssssssssssssi', $_POST[fname], $_POST[lname], $_POST[orgName], $_POST[address], $_POST[city], $_POST[state], $_POST[zipcode], $_POST[phone], $_POST[fax], $_POST[email], $_POST[confirmEmail], $_POST[projectOptions], $_POST[projectOverview], $_POST[year]); $stmt -> execute(); $stmt -> close(); } ?> </head> <body> <form action ="contactTest.php" method="post"> <label> <input id = "fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php echo !empty($_POST['fname']) ? $_POST['fname'] : '';?>" > <?php echo !empty($error['fname']) ? $error['fname'] : '';?> <input type="text" name="lname" size="20" placeholder="Last Name"><?php echo !empty($error['lname']) ? $error['lname'] : '';?> <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50"> </label><br /> <label> <!--new row --> <input id = "address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50"> <input id = "city" type="text" name="city" placeholder="City" size="10" maxlength="25"> <select id = "state" name = "state" placeholder="State" value=""> <option value ="Please choose a state"> Please choose a state </option> <?php states($state); ?> </select> <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5"> </label><br /> <label> <!--new row --> <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10"> </label> <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10"> </label><br /> <label> <!--new row--> <input type="text" id = "email" name="email" placeholder="Email Address" /> <input type="text" id = "confirmEmail" name="ConfirmEmail" placeholder="Confirm Email Address" /> </label><br /> <label> <!--new row --> What would you like help with? <table id="projectOptions"> <tr span=2> <td><input type="checkbox" name="SocialMedia">Social Media </td> <td><input type="checkbox" name="WebContentManagement">Web Content Management </td> </tr> <tr> <td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation </td> <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) </td> </tr> <tr> <td><input type="checkbox" name="VideoEditing"> Video Editing </td> <td><input type="checkbox" name="WebDesign">Web Design </td> </tr> </table> </label> Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br /> If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br /> <input type="submit" name="submit" value="Contact Me!"> <input type="reset" name="reset" value="Cancel"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/299539-one-last-error/ Share on other sites More sharing options...
Barand Posted November 20, 2015 Share Posted November 20, 2015 (edited) Your connection object is $db, but you are trying to use $mysqli->prepare() Edited November 20, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/299539-one-last-error/#findComment-1526861 Share on other sites More sharing options...
mac_gyver Posted November 20, 2015 Share Posted November 20, 2015 (edited) you are also making a PDO database connection, but using mysqli statements. you cannot mix the different database api's. you must use all PDO statements to match what you used when you made the database connection. edit: you are also making the database connection inside of a user written function, but are not passing the connection back to the calling code, so whatever variable you assign the connection to won't be available in any case. if you are going to wrap the connection code in a user written function, you need to return the connection to the calling code and assign it to a variable in the calling code's scope. Edited November 20, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/299539-one-last-error/#findComment-1526862 Share on other sites More sharing options...
mindapolis Posted November 20, 2015 Author Share Posted November 20, 2015 I'm so sorry. I'm brand new to PDO. I will work on it tonight. Thanks so much for being nice about it. Quote Link to comment https://forums.phpfreaks.com/topic/299539-one-last-error/#findComment-1526865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.