• Attempting to prepare a previously working insert statement. The error is on line 42 reporting a null on $query. The echo query and Values are not null, but print_r($link) fails, altho the link was successful initially.

    <?php
    			$link = mysqli_connect("localhost", "hsysgrpc_WPHZU", "xxxxxxxx, "xxxxxxxx"); 
    			print_r($Link);
    			if(!$link) {    
    			die ("ERROR: Could not connect. " . mysqli_connect_error() );
    			} 
    			echo "Connected successfully";
    			
    			$ID = $_POST['ID'];
    			print_r($ID);
    			$Title = $_POST['Title'];
    			print_r($Title);
    			$FirstName = $_POST['FirstName'];
    			print_r($FirstName);
    			$LastName = $_POST['LastName'];
    			print_r($LastName);
    			$Address1 = $_POST['Address1'];
    			print_r($Address1);
    			$City = $_POST['City'];
    			print_r($City);
    			$State = $_POST['State'];
    			print_r($State);
    			$Zip = $_POST['Zip'];
    			print_r($Zip);
    			$HomePhone = $_POST['HomePhone'];
    			print_r($HomePhone);
    			$CellPhone = $_POST['CellPhone'];
    			print_r($CellPhone);
    			$Email = $_POST['Email'];
    			print_r($Email);
    			$College1 = $_POST['College1'];
    			print_r($College1);
    			$College2 = $_POST['College2'];
    			print_r($College2);
    			$College3 = $_POST['College3'];
    			print_r($College3);
    		
      			global $wpdb;
    			//$wpdb->show_errors(true);
    
    	$query = "INSERT INTO AAUW_Members (ID, FirstName, LastName, Address1, City, State, Zip)	Values (%s, %s, %s, %s, %s, %s,  %s )";
    			echo "$query";
    	print_r ($link);
    	echo "$ID";
    	echo "$FirstName";
    	echo "$LastName";
    	echo "$Address1";
    	echo "$City";
    	echo "$State";
    	echo "$Zip";
    	$prepare_query = $wpdb->prepare($query, $ID, $FirstName, $LastName, $Address1, $City, $State, $Zip);
    			$result=$wpdb->query($prepare_query);
    					
    			print_r($result);			
    ?>

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Your first print_r() has an upper case ‘L’ in Link: print_r($Link);
    Most server systems are case sensitive, $link != $Link.

    There should be an undefined variable Link error. Lines 41 and 42 extracted on their own don’t cause any error, so I think the message is a red herring due to some earlier error.

    Dion

    (@diondesigns)

    Initially you are bypassing WordPress by using mysqli directly to connect to a database. (One hopes the credentials you posted are incorrect; if not, please consider changing them ASAP.) Then you ignore the DB connection you created and attempt to use $wpdb.

    My suggestion is to choose one (mysqli or $wpdb) and only use the one you choose. If you choose mysqli, then make sure to sanitize the contents of the POST variables before attempting to insert them into the database. And give some thought to insuring you have a unique index in your DB table, then using REPLACE instead of INSERT in the query.

    Thread Starter hsysgrp

    (@hsysgrp)

    The page accepts data and prints out $ID, $FirstName, etc. Have removed mysqli_connect to link, based on other testing the global wpdb is linked to the custom table;
    Call to a member function show_errors() on null is the error reported, I am missing something basic here…also can you erase the previous link info in my previous post, sorry for that lapse, I forgot to edit it.

    <?php
    						
    			$ID = $_POST['ID'];
    			print_r($ID);
    			$Title = $_POST['Title'];
    			print_r($Title);
    			$FirstName = $_POST['FirstName'];
    			print_r($FirstName);
    			$LastName = $_POST['LastName'];
    			print_r($LastName);
    			$Address1 = $_POST['Address1'];
    			print_r($Address1);
    			$City = $_POST['City'];
    			print_r($City);
    			$State = $_POST['State'];
    			print_r($State);
    			$Zip = $_POST['Zip'];
    			print_r($Zip);
    			$HomePhone = $_POST['HomePhone'];
    			print_r($HomePhone);
    			$CellPhone = $_POST['CellPhone'];
    			print_r($CellPhone);
    			$Email = $_POST['Email'];
    			print_r($Email);
    			$College1 = $_POST['College1'];
    			print_r($College1);
    			$College2 = $_POST['College2'];
    			print_r($College2);
    			$College3 = $_POST['College3'];
    			print_r($College3);
    		
      			global $wpdb;
    			$wpdb->show_errors(true);
    
    	$query = "INSERT INTO AAUW_Members (ID, FirstName, LastName, Address1, City, State, Zip)	Values (%s, %s, %s, %s, %s, %s,  %s )";
    			echo "$query";
    	
    	echo "$ID";
    	echo "$FirstName";
    	echo "$LastName";
    	echo "$Address1";
    	echo "$City";
    	echo "$State";
    	echo "$Zip";
    	$prepare_query = $wpdb->prepare($query, $ID, $FirstName, $LastName, $Address1, $City, $State, $Zip,"");
    			$result=$wpdb->query($prepare_query);
    					
    			echo	"print_r($result)";
    ?>
    Dion

    (@diondesigns)

    A moderator must edit your initial post to remove the DB info. I added the “modlook” tag in this reply, which will hopefully flag a moderator.

    If you are loading your PHP script directly (either via AJAX or a form submission), then $wpdb doesn’t exist because WordPress isn’t loaded. In that case the best solution is to use mysqli functions to connect to the database, sanitize the POST variables, and submit the query.

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you, now the null error is beginning to make sense. The website page has a form the form action calls insert.php to insert the data into a custom table in the wp database. Can I include the load wp-load.php instead of linking with mysqli_connect? I thought it was best practice to sanitize with the wpdb prepare functions? How do I connect to my wp database to use the wpdb functions? I see directions to connect to another database, but not the one I thought I was in???
    Still learning here, as I think is obvious.

    Dion

    (@diondesigns)

    It’s best practice to sanitize GET/POST data meant to be added to a database. The choice of $wpdb->prepare() on the query or mysqli_real_escape_string() on each variable is up to you. Personally, with only a single query, you are much better off using mysqli_ functions…loading WordPress (including the theme and all plugins) for such a simple task is beyond overkill!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Call to a member function prepare() on null’ is closed to new replies.