Welcome to WebmasterWorld Guest from 52.91.176.251

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP MySQLi INSERT

undefined variable $conn

     
2:50 am on Oct 28, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


Hi everyone:

Getting a notice that my variable $conn is undefined and a fatal error that my call on a member function query() is on a non-object. At this stage, I'm attempting to just insert dummy data into my MySQL database before moving on to secure the page and go on to build the rest of the registry system.

 //MY DBCONNECT INCLUDE FILE
<?php
$conn = new mysqli("localhost", "", "", "occupus_Registration_Database");
if($conn->connect_error) {
die("Faided To Connect To Database:" . $conn->connect_error);
}
?>


 //THE PHP ON MY FORM'S PAGE
<?php require 'dbconnect.php'; ?>
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
?>
<?php
if (isset($_POST['osformsub'])) {
session_start();
$LastName = $_POST['oslast'];
$FirstName = $_POST['osfirst'];
$Midi = $_POST['osmidi'];
$Address1 = $_POST['osaddone'];
$Address2 = $_POST['osaddtwo'];
$City = $_POST['oscity'];
$State = $_POST['osstate'];
$Zip = $_POST['oszip'];
$ZipFour = $_POST['osplusfour'];
$AttnLast = $_POST['osshlast'];
$AttnFirst = $_POST['osshfirst'];
$AttnMidi = $_POST['osshmidi'];
$ShAdd1 = $_POST['osshaddone'];
$ShAdd2 = $_POST['osshaddtwo'];
$ShCity = $_POST['osshcity'];
$ShState = $_POST['osshstate'];
$ShZip = $_POST['osshzip'];
$ShZip4 = $_POST['osshplusfour'];
$Email = $_POST['osemail'];
$Pasword = $_POST['ospass'];
$sql = $conn->query("INSERT INTO registration (LastName, FirstName, Midi, Address1, Address2, City, State, Zip, ZipFour, AttnLast, AttnFirt, AttnMidi, ShAdd1, ShAdd2, ShCity, ShState, ShZip, ShZip4, Email, Password) VALUES ('{$LastName}', '{$FirstName}', '{$Midi}', '{$Address1}', '{$Address2}', '{$Zip}', '{$ZipFour}', '{$AttnLast}', '{$AttnFirst}', '{$AttnMidi}', '{$ShAdd1}', '{$ShAdd2}', '{$ShCity}', '{$ShState}', '{$ShZip}', '{$ShZip4}', '{$Email}', '{$Password}')");
$conn->close;
}
?>
3:03 am on Oct 28, 2015 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


Welcome to WebmasterWorld RockSquirrel,

my guess is you are getting an error on your query, a good way to test that is to construct your query before you pass it to the query method, then you have the option of echoing the query and pasting it into phpmyadmin, workbench, the command line or whatever you use for mysql, to test the query itself. You can also die the method like you did with the connect method

$q = "INSERT INTO registration (LastName, FirstName, Midi, Address1, Address2, City, State, Zip, ZipFour, AttnLast, AttnFirt, AttnMidi, ShAdd1, ShAdd2, ShCity, ShState, ShZip, ShZip4, Email, Password) VALUES ('{$LastName}', '{$FirstName}', '{$Midi}', '{$Address1}', '{$Address2}', '{$Zip}', '{$ZipFour}', '{$AttnLast}', '{$AttnFirst}', '{$AttnMidi}', '{$ShAdd1}', '{$ShAdd2}', '{$ShCity}', '{$ShState}', '{$ShZip}', '{$ShZip4}', '{$Email}', '{$Password}')";

echo $q;

$sql = $conn->query($q);
10:06 am on Oct 28, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts:322
votes: 24


If $conn is undefined then it would seem that "MY DBCONNECT INCLUDE FILE" is not being included? Do you have another file called "dbconnect.php" that is perhaps being included instead? (Since you are specifying a relative include it will be searching the include_path for this file.)

Even if the DB connection failed $conn should still at least be defined.

error_reporting(E_ALL);
ini_set('display_errors','1');


Move your error_reporting calls to the very start of your script, so you capture all errors. At the moment it is only set after including your dbconnect.php file, so you might be missing something. Also, depending on your version of PHP, include E_STRICT also (at least whilst in development):


error_reporting(E_ALL | E_STRICT);


Also, don't keep closing and opening PHP sections - this is prone to error (eg. if you have any erroneous characters between sections that results in output). This is all PHP code so you should only be opening the PHP section at the very start of the file.
7:24 pm on Oct 28, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


Thanks all. I'll try these suggestions and post back the results.
1:25 am on Oct 30, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


Hi again all. Okay, here's the results. I checked my inclusion by just misspelling some data like my username or putting in an incorrect password. I would get the expected "Failed to Connect" warning with the associated errors, so my first thought is that the file is being included into the page that has my form. Prior to that, I coded the suggestions by Whitespace and Jatar K. (I appreciate the help, guys.) The actual code is below, except that I took out my echo of $q which seemed to be doing what it was supposed to. The strange phenomenon is right there on the $conn variable...lol. I took out the "require" at one point and just coded my connection script right into my forms page. All errors went away but it did not physically insert data into MySQL (I use PHPMyAdmin 4.3.8 - there's a newer version available and I will ask my host to install it, if available). Otherwise, it seems to connect and include fine with the exception of the notice of undefined $conn. After this post, I will check all my user settings for my host (HostGator), my MySQL (i.e.: did I set up my database and use right) and any user settings for my version of PHPMyAdmin.

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors','1');
require 'http://www.occubusinessandleisure.com/Includes/dbconnect.php';
if (isset($_POST['osformsub'])) {
session_start();
$LastName = $_POST['oslast'];
$FirstName = $_POST['osfirst'];
$Midi = $_POST['osmidi'];
$Address1 = $_POST['osaddone'];
$Address2 = $_POST['osaddtwo'];
$City = $_POST['oscity'];
$State = $_POST['osstate'];
$Zip = $_POST['oszip'];
$ZipFour = $_POST['osplusfour'];
$AttnLast = $_POST['osshlast'];
$AttnFirst = $_POST['osshfirst'];
$AttnMidi = $_POST['osshmidi'];
$ShAdd1 = $_POST['osshaddone'];
$ShAdd2 = $_POST['osshaddtwo'];
$ShCity = $_POST['osshcity'];
$ShState = $_POST['osshstate'];
$ShZip = $_POST['osshzip'];
$ShZip4 = $_POST['osshplusfour'];
$Email = $_POST['osemail'];
$Password = $_POST['ospass'];
$q = "INSERT INTO registration (LastName, FirstName, Midi, Address1, Address2, City, State, Zip, ZipFour, AttnLast, AttnFirt, AttnMidi, ShAdd1, ShAdd2, ShCity, ShState, ShZip, ShZip4, Email, Password) VALUES ('{$LastName}', '{$FirstName}', '{$Midi}', '{$Address1}', '{$Address2}', '{$Zip}', '{$ZipFour}', '{$AttnLast}', '{$AttnFirst}', '{$AttnMidi}', '{$ShAdd1}', '{$ShAdd2}', '{$ShCity}', '{$ShState}', '{$ShZip}', '{$ShZip4}', '{$Email}', '{$Password}')";
$sql = $conn->query($q);
}
?>
11:17 am on Oct 30, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts:322
votes: 24


require 'http://www.occubusinessandleisure.com/Includes/dbconnect.php';


This will definitely not work and will most certainly result in a "$conn is undefined" notice later in your script.

By including an absolute URL (HTTP protocol, hostname, etc) you are making an external request. This is very different from using a local filesystem path. By using an absolute URL you are only including the output of the included file - ie. nothing in this case!

$sql = $conn->query($q);


You should also be checking for failure. query() will return False on error. Then check $conn->error.
7:12 pm on Oct 30, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


That was it! Thanks Whitespace. The errors went away. I still don't get physical submission into the database, and I will add the error checking on the query that you and Jatar suggested. Would it also be my settings in PHPMyAdmin or MySQL, if there is such a thing. I never tinkered in those areas, leaving whatever settings that came as default as is.
9:13 pm on Oct 30, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts:322
votes: 24


That was it!


Well, there must have been something else?! You never included an absolute URL in your original post and that supposedly gave you the same error? Your two scripts would seem to be "the same" - at least from this end!?
6:50 pm on Oct 31, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


Correct. I see the confusion. The two are the same. The first time, I was being guarded and I truncated the full url when I made my post (posted the true script and then erased what I felt was irrelevant or unnecessary even though this is just a development stage). Ah, but the joke was on me as the original post seemed to tell everyone that I was requesting locally and I wasn't. So...and I apologize...you were first trying to figure things on a bogus post - so to speak. Long story short, had I posted the PHP completely as the way I was developing it, we would have got to the solution sooner...lol. Lesson learned. Side note: I was unaware, but intrigued to find out the importance of an external request verses an internal one. So now - and I apologize for the redundancy - I have no errors, but no submission either. My forms page just refreshes after I click submit (I expect that part because I don't have any direction for the page to do anything else at this time). When I echo my query before passing it to the method, the output is the values of the variables, so I assume it is constructed correctly. For some strange phenomenon, I don't get physical insertion into "registration." Now, if I copy the INSERT INTO command and associated code directly from the above script into PHPMyAdmin (as opposed to manually filling in the values inside the actual table), it works. My thinking, and the reason I asked about settings and so forth was, PHP still does not know where to insert the data. At the time of this post, I still haven't checked for failures as you suggested...just starting my day. I will do so now after this post.
2:21 am on Nov 1, 2015 (gmt 0)

New User

joined:Oct 26, 2015
posts: 3
votes: 0


And the answer is: my column count did not match my value. Whew...long day...lol. Here's a little more redundancy for anyone who may stumble onto my thread...the key here, I think, was putting in error and failure checks all along during the development stage. Okay. On to the next stage. Thanks everyone, I really appreciate all the help.
8:48 pm on Nov 1, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts:322
votes: 24


Glad you got it sorted and thanks for the feedback; much appreciated.

> "And the answer is: my column count did not match my value."
But I thought you'd copy/pasted your SQL into PhpMyAdmin to test - which worked? Anyway... ;)

(ASIDE: "posts: 3" - but you've made at least 6 posts in this thread alone? Hhhmm, forum "glitch"? Actually, I don't think my post count is increasing either?)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members