Forum Replies Created

Viewing 15 replies - 16 through 30 (of 111 total)
  • Thread Starter hsysgrp

    (@hsysgrp)

    Exactly right, thank you.
    Now I have:
    $result_check = $wpdb->update(‘AAUW_Members’,
    array(
    ‘ID’ => $ID,
    ‘Title’ => $Title,
    ‘FirstName’ => $FirstName,

    ),
    array(
    ‘ID’ => $ID
    )
    );

    This worked after I removed the 29 types %d%, %s%, array. It is not critical here as the ID is an integer, but not calculated with. However, where do I insert the type array in this syntax if it is needed?

    Thread Starter hsysgrp

    (@hsysgrp)

    $sql = “UPDATE AAUW_Members SET ID = ‘$ID’, Title = ‘$Title’, FirstName = ‘$FirstName’, LastName = ‘$LastName’, Address1 = ‘$Address1’, City = ‘$City’, State = ‘$State’, Zip = ‘$Zip’, HomePhone = ‘$HomePhone’, CellPhone = ‘$CellPhone’, Email = ‘$Email’, College1 = ‘$College1’, College2 = ‘$College2′, College3 =’$College3’,Birth_Day = ‘$Birth_Day’, Birth_Month = ‘$Birth_Month’, Mem_Type = ‘$Mem_Type’, Honorary = ‘$Honorary’, Joined_Local = ‘$Joined_Local’, Joined_Natl = ‘$Joined_Natl’, Mailings = ‘$Mailings’, Positions_Held = ‘$Positions_Held’, Notes = ‘Notes’,Referred = ‘$Referred’, Retired = ‘$Retired’, Employer = ‘$Employer’,Occupation = ‘$Occupation’, Positions = ‘$Positions’, OtherWriteIn = ‘$OtherWriteIn’
    WHERE ID = ‘$ID’ “;
    Thread Starter hsysgrp

    (@hsysgrp)

    Hi Steve, thank you. the SQL is from a working custom page used to update member data on a WordPress website. The tablename is AAUW_Members. The error dump I posted complains about unknown column ‘0’. But column 0 is ‘ID’=>$ID which is matched with ‘%d%’ because ID is an integer. `$sql = “UPDATE AAUW_Members SET ID = ‘$ID’, Title = ‘$Title’, FirstName = ‘$FirstName’, LastName = ‘$LastName’, Address1 = ‘$Address1’, City = ‘$City’, State = ‘$State’, Zip = ‘$Zip’, HomePhone = ‘$HomePhone’, CellPhone = ‘$CellPhone’, Email = ‘$Email’, College1 = ‘$College1’, College2 = ‘$College2′, College3 =’$College3’,Birth_Day = ‘$Birth_Day’, Birth_Month = ‘$Birth_Month’, Mem_Type = ‘$Mem_Type’, Honorary = ‘$Honorary’, Joined_Local = ‘$Joined_Local’, Joined_Natl = ‘$Joined_Natl’, Mailings = ‘$Mailings’, Positions_Held = ‘$Positions_Held’, Notes = ‘Notes’,Referred = ‘$Referred’, Retired = ‘$Retired’, Employer = ‘$Employer’,Occupation = ‘$Occupation’, Positions = ‘$Positions’, OtherWriteIn = ‘$OtherWriteIn’
    WHERE ID = ‘$ID’ “;`

    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    Success! thank you all. Below is the complete file, if that will help anybody else. To review, this file is called by a form to download data displayed on a webpage. The difficulty was that $wpdb was not loading, hence the call to wp-load.php. Additionally, the header information had to be placed above the require line or the output file was named “DownloadCommitteesFile.php” for reasons totally obscure to me.

    <?php
    //DownloadCommitteesFile.php 3/21/2022
    //Called by form on custom-page_Download_Committees-wpdb.php
    header('Content-Type: text/csv');
    
    $filename = 'Committees.csv';
    
    header("Content-Disposition: attachment; filename= $filename " );
    
    $output = fopen('php://output', 'w');
    require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
    
    global $wpdb;
    if (!defined('ABSPATH') || empty($wpdb)) {die('WordPress not loaded.');}
    $wpdb->show_errors(true);
    include 'myFuncs.php' ;
    $link = connect() ;
    if($link === false){    die("ERROR: Could not connect. " . mysqli_connect_error());} 
    	
    $SQL =	"SELECT FirstName, LastName, AAUWDatabase, DiversityInclusion, InternationalRelations, Membership, 
    	ProgramPlanning, PublicPolicy, Website, Zoom FROM AAUW_Members WHERE (((AAUWDatabase)=1)) OR (((DiversityInclusion)=1)) OR (((InternationalRelations)=1))
    	 OR (((Membership)=1)) OR (((ProgramPlanning)=1)) OR (((PublicPolicy)=1)) OR (((Website)=1)) OR (((Zoom)=1)) ORDER BY Lastname ";
    	
    	$result = $wpdb->get_results($SQL, ARRAY_A
    );
    
    // Download file header record
    
    fputcsv($output, array(
    	'FirstName',
    	'LastName',
    	'AAUWDatabase',
    	'DiversityInclusion',
    	'InternationalRelations',
    	'Membership',
    	'ProgramPlanning',
    	'PublicPolicy',
    	'Website',
    	'Zoom',
    	));
    
    foreach($result as $row){
    	
    	fputcsv($output, array(
    		$row['FirstName'],
    		$row['LastName'],
    		$row['AAUWDatabase'],
    		$row['DiversityInclusion'],
    		$row['InternationalRelations'],
    		$row['Membership'],
    		$row['ProgramPlanning'],
    		$row['PublicPolicy'],
    		$row['Website'],
    		$row['Zoom'],
    	));	
    		
    } //end foreach
    
    fclose($output);
    
    ?>
    • This reply was modified 2 years, 8 months ago by Jan Dembowski. Reason: Formatting
    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    placing the header statement before the $output = fopen() line elicits an error saying headers are already sent.

    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you. OK. I moved the header statement to above the “require($_SERVER[‘DOCUMENT_ROOT’] . ‘/wp-load.php’);” statement and ran it. I got a downloaded file named “DownloadCommitteesFile.php”. I manually changed the name to “CommitteesFile.csv” and saved it, and voila! got my desired output. I don’t know JS. How can I set my desired filename programmatically?

    <?php
    //DownloadCommitteesFile.php 3/19/2022
    header('Content-Type: text/csv');
    require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
    //$path = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php";
    //echo $path;
    global $wpdb;
    if (!defined('ABSPATH') || empty($wpdb)) {die('WordPress not loaded.');}
    $wpdb->show_errors(true);
    include 'myFuncs.php' ;
    $link = connect() ;
    if($link === false){    die("ERROR: Could not connect. " . mysqli_connect_error());} 
    
    $FileName = 'Content-Disposition: attachment; filename="'. 'Committees.csv"';
    	
    $SQL =	"SELECT FirstName, LastName, AAUWDatabase, DiversityInclusion, InternationalRelations, Membership, 
    	ProgramPlanning, PublicPolicy, Website, Zoom FROM AAUW_Members WHERE (((AAUWDatabase)=1)) OR (((DiversityInclusion)=1)) OR (((InternationalRelations)=1))
    	 OR (((Membership)=1)) OR (((ProgramPlanning)=1)) OR (((PublicPolicy)=1)) OR (((Website)=1)) OR (((Zoom)=1)) ORDER BY Lastname ";
    	
    	$result = $wpdb->get_results($SQL, ARRAY_A
    );
    
    $output = fopen('php://output', 'w');
    
    // Download file header record
    
    fputcsv($output, array(
    	'FirstName',
    	'LastName',
    	'AAUWDatabase',
    	'DiversityInclusion',
    	'InternationalRelations',
    	'Membership',
    	'ProgramPlanning',
    	'PublicPolicy',
    	'Website',
    	'Zoom',
    	));
    
    foreach($result as $row){
    	
    	fputcsv($output, array(
    		$row['FirstName'],
    		$row['LastName'],
    		$row['AAUWDatabase'],
    		$row['DiversityInclusion'],
    		$row['InternationalRelations'],
    		$row['Membership'],
    		$row['ProgramPlanning'],
    		$row['PublicPolicy'],
    		$row['Website'],
    		$row['Zoom'],
    	));	
    		
    } //end foreach
    
    fclose($output);						  		 	
    ?>
    
    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    Oh, we are getting close! WordPress loaded, output for the csv file is listed, but: error message:
    Warning: Cannot modify header information – headers already sent by (output started at /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/myFuncs.php:13) in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php on line 12

    Warning: Cannot modify header information – headers already sent by (output started at /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/myFuncs.php:13) in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php on line 14
    FirstName,LastName,AAUWDatabase,DiversityInclusion,InternationalRelations,Membership,ProgramPlanning,PublicPolicy,Website,Zoom Lula,Allen,0,1,0,0,0,0,0,0 etc.

    myFuncs.php contains the connect function.
    I commented out the header ($FileName) and got the same error. I think it is the order in which the statements are added?, but I am ignorant here.

    <?php
    //DownloadCommitteesFile.php 3/19/2022
    require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
    //$path = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php";
    //echo $path;
    global $wpdb;
    if (!defined('ABSPATH') || empty($wpdb)) {die('WordPress not loaded.');}
    $wpdb->show_errors(true);
    include 'myFuncs.php' ;
    $link = connect() ;
    if($link === false){    die("ERROR: Could not connect. " . mysqli_connect_error());} 
    header('Content-Type: text/csv');
    $FileName = 'Content-Disposition: attachment; filename="'. 'Committees.csv"';
    //header($FileName);
    	
    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    I attempted to provide a path so $wpdb would load, and got the
    “/home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.phpWordPress not loaded” error.
    I added the $path statement but was not successful, can you give me more guidance?
    <?php
    //DownloadCommitteesFile.php 3/17/2022
    $path = $_SERVER[‘DOCUMENT_ROOT’] . “/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php”;
    echo $path;
    global $wpdb;
    if (!defined(‘ABSPATH’) || empty($wpdb)) {die(‘WordPress not loaded.’);}
    $wpdb->show_errors(true);
    include ‘myFuncs.php’ ;
    $link = connect() ;
    if($link === false){ die(“ERROR: Could not connect. ” . mysqli_connect_error());}
    header(‘Content-Type: text/csv’);
    $FileName = ‘Content-Disposition: attachment; filename=”‘. ‘Committees.csv”‘;
    header($FileName);

    $SQL = “SELECT FirstName, LastName, AAUWDatabase, DiversityInclusion, InternationalRelations, Membership,
    ProgramPlanning, PublicPolicy, Website, Zoom FROM AAUW_Members WHERE (((AAUWDatabase)=1)) OR (((DiversityInclusion)=1)) OR (((InternationalRelations)=1))
    OR (((Membership)=1)) OR (((ProgramPlanning)=1)) OR (((PublicPolicy)=1)) OR (((Website)=1)) OR (((Zoom)=1)) ORDER BY Lastname “;

    $result = $wpdb->get_results($SQL, ARRAY_A
    );

    Forum: Developing with WordPress
    In reply to: $wpdb
    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you, You are right, I got ‘WordPress not loaded’. So how do I load it, I have tried
    include ‘../wp-load.php’; and got error include(): Failed opening ‘../wp-load.php’ for inclusion (include_path=’.:/opt/cpanel/ea-php73/root/usr/share/pear’) in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php on line 3

    Also, I am working in a WordPress website; the code for a page on the website contains
    global $wpdb; displays the results of $wpdb->get_results(…
    the results are displayed,
    a form is displayed:

    <form method = “post” action = “/wp-content/themes/twentytwelve-child/DownloadCommitteesFile.php”>
    <input type = “submit” name = “export” value = “CSV Export” />
    </form>
    DownloadCommitteesFile.php is called from the /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/folder (from the code listed above)
    so what point did we lose $wpdb, and why?

    Forum: Fixing WordPress
    In reply to: $wpdb not loading?
    Thread Starter hsysgrp

    (@hsysgrp)

    I am using a twentytwelve child theme. I have pages created in the usual WordPress manner using the full-width template. One page uses a template I wrote, custom-page_InputMemberID-wpdb.php. This is the code I provided in my first message. I have not made any special calls to the WordPress environment other than the global $wpdb;

    Forum: Fixing WordPress
    In reply to: $wpdb not loading?
    Thread Starter hsysgrp

    (@hsysgrp)

    Site hsysgrp.com
    Page New Member
    sub Page Input Member ID
    Permalink [ redundant link removed ]
    Template Name: Input Member ID wpdb
    custom-page_InputMemberID-wpdb.php
    I have pages where the get_results SELECT method works fine, both INSERT and UPDATE statements give me the same call to a member function on null error. I used to think it was syntax, now I’m really puzzled.

    Thread Starter hsysgrp

    (@hsysgrp)

    The error I get is Call to a member function update() on null in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_InputMemberID-wpdb.php:51
    Stack trace:
    I have tried without success
    require_once(‘../wp-load.php’); // relative path
    global $wpdb;

    $wpdb->show_errors(true); also

    $updated = $wpdb->update($tablename,
    array(‘MemberID’ => $searchn
    ),
    array(‘LastName’ =>$searchq
    )
    );
    echo print_r($updated);

    Thread Starter hsysgrp

    (@hsysgrp)

    INSERT INTO AAUW_Members (ID, Title, FirstName,LastName,Address1,City, State, Zip) VALUES ('111114', 'Ms.', 'Rosalind', 'Franklin', '10 Birkdale', 'Poughkeepsie', 'NY', '12603')  This sql works in phpmyadmin.
    
    	
    $query2 = "Insert into AAUW_Members ( ID, Title, FirstName,LastName, Address1, City, State, Zip) Values (%d,%s,%s,%s,%s,%s,%s,%s)";
    
    $prepare_query = $wpdb->prepare($query2,$ID,$Title,$FirstName,$LastName,$Address1, $City, $State, $Zip);
    $result = $wpdb->query($prepare_query);
    		echo print_r($result);
    PHP Fatal error:  Uncaught Error: Call to a member function prepare() on null in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_RestoreID_wpdb.php:121

    Don’t know what that means..

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you for your response. The context is the original template file custom-page_RestoreID.php searches for a record in an archive table and inserts that record into the members table using “$sqlInsert .= “INSERT INTO AAUW_Members ( ID, Title, FirstName, LastName, Address1, ….”. I am attempting to convert sql statements into $wpdb format for security reasons. The template file custom-page_RestoreID_wpdb.page replaces $sqlInsert with “$query2 = “INSERT INTO AAUW_Members ( ID, Title, FirstName, LastName, Address1,…. $prepare_query = $wpdb->prepare($query2, $ID, $Title, $FirstName, $LastName, $Address1, etc. I know I am missing something, as it appears the insert is not executing at all. Self-taught here, appreciate all the help.

    Thread Starter hsysgrp

    (@hsysgrp)

    Am replacing straight sql with wpdb functions. Code works up to the global $wpdb; then nothing.`
    $sql = “SELECT ID, Title, FirstName,LastName, Address1, City, State, Zip, HomePhone, CellPhone, Email, “;
    $sql .= ” College1, College2, College3, Birth_Day, Birth_Day, Birth_Month, Mem_Type, Honorary, Joined_Local, “;
    $sql .= “Joined_Natl, Mailings “;

    $sql .= “FROM AAUW_Archived WHERE ID = ‘$ID’ “;

    echo $sql, ‘<br>’;

    $retrieve_data = mysqli_query($link, $sql);
    if (mysqli_num_rows($retrieve_data) > 0)
    { while($row = mysqli_fetch_array($retrieve_data)) {

    echo “ID: ” . $row[“ID”]. “<br>”;
    echo “Title: ” . $row[“Title”] . “<br>”;
    echo “FirstName: ” . $row[“FirstName”] . “<br>”;
    echo “LastName: ” . $row[“LastName”] . “<br>”;
    echo “Address1: ” . $row[“Address1”] . “<br>”;
    echo “City: ” . $row[“City”] . “<br>”;
    echo “State: ” . $row[“State”] . “<br>”;
    echo “Zip: ” . $row[“Zip”] . “<br>”;
    echo “HomePhone: ” . $row[“HomePhone”] . “<br>”;
    echo “CellPhone: ” . $row[“CellPhone”] . “<br>”;
    echo “Email: ” . $row[“Email”] . “<br>”;
    echo “College1: ” . $row[“College1”] . “<br>”;
    echo “College2: ” . $row[“College2”] . “<br>”;
    echo “College3: ” . $row[“College3”] . “<br>”;
    echo “Birth_Day: ” . $row[“Birth_Day”] . “<br>”;
    echo “Birth_Month: ” . $row[“Birth_Month”] . “<br>”;
    echo “Mem_Type: ” . $row[“Mem_Type”] . “<br>”;
    echo “Honorary: ” . $row[“Honorary”] . “<br>”;
    echo “Joined_Local: ” . $row[“Joined_Local”] . “<br>”;
    echo “Joined_Natl: ” . $row[“Joined_Natl”] . “<br>”;
    echo “Mailings: ” . $row[“Mailings”] . “<br>”;

    $ID = $row[‘ID’];

    $Title = $row[‘Title’];
    $FirstName = $row[‘FirstName’];
    $LastName = $row[‘LastName’];
    $Address1 = $row[‘Address1’];
    $City = $row[‘City’];
    $State = $row[‘State’];
    $Zip = $row[‘Zip’];
    $HomePhone = $row[‘HomePhone’];
    $CellPhone = $row[‘CellPhone’];
    $Email = $row[‘Email’];
    $College1 = $row[‘College1’];
    $College2 = $row[‘College2’];
    $College3 = $row[‘College3’];
    $Birth_Day = $row[‘Birth_Day’];
    $Birth_Month = $row[‘Birth_Month’];
    $Mem_Type = $row[‘Mem_Type’];
    $Honorary = $row[‘Honorary’];
    $Joined_Local = $row[‘Joined_Local’];
    $Joined_Natl = $row[‘Joined_Natl’];
    $Mailings = $row[‘Mailings’];

    echo “Variables”;
    } // while mysqli_fetch…retrieve_data
    } else { // else of retrieve_data > 0
    echo “0 results”;
    }
    } // else of while mysqli_fetch…query
    // Sql to Insert into AAUW_Members using wpdb
    echo $ID;
    echo $Title;
    echo $FirstName;

    global $wpdb;
    $wpdb->show_errors(true);
    $query2 = “INSERT INTO AAUW_Members ( ID, Title, FirstName, LastName, Address1, City, State, Zip, HomePhone, CellPhone, Email,
    College1, College2, College3, Birth_Day, Birth_Month, Mem_Type, Honorary, Joined_Local,Joined_Natl, Mailings)
    Values (%d, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)” ;
    $prepare_query = $wpdb->prepare($query2, $ID, $Title, $FirstName, $LastName, $Address1, $City, $State, $Zip, $HomePhone, $CellPhone, $Email,
    $College1, $College2, $College3, $Birth_Day, $Birth_Month, $Mem_Type, $Honorary, $Joined_Local, $Joined_Natl, $Mailings);
    $result = $wpdb->query($prepare_query);
    echo print_r($wpdb);

    } // else of count == 0
    } //Close isset search
    `

Viewing 15 replies - 16 through 30 (of 111 total)