• Resolved hsysgrp

    (@hsysgrp)


    <?php
    // HFA _Members _Update
    // Custom-page_HFA_Members_Update.php 2/28/2023
    /**
    
     * Template Name: HFA Members Update Profile
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    include 'myFuncs.php';
    $link = connect();
    global $wpdb;
    $wpdb->show_errors(true);
    
    ?>
    <html>
    <body>
    
    <center>
    <h2> View or Edit Members </h2>
    
    
    <form method="POST"> 
    	
    	<input type ="text" name = "fname" placeholder= "Search for FirstName..."/>
    	<input type ="text" name = "lname" placeholder= "Search for LastName..."/>
    	<input type="submit" name="search" value="Search" />
    </form>
    
    <?php
    	$output = null;
           		 $searchf = $_POST['fname'];	
    		$searchl = $_POST['lname'];
    //		$searchf = preg_replace("#[^0-9a-z]#i","",$searchf);	
    //		$searchl = preg_replace("#[^0-9a-z\']#i","",$searchl);		
    
    		$query = mysqli_query($link,"SELECT ID,  FirstName, LastName FROM HFA_Members WHERE FirstName LIKE  '%$searchf%' AND Lastname LIKE '%$searchl%'") OR die("Could not  search!"); 
    	
    		$row = mysqli_fetch_array($query);
    		$FirstName = $row['FirstName']; 
    		$LastName = $row['LastName'] ;
    		$ID = $row['ID'];
    		$output = '<div>'.$ID.' ' .$FirstName.' '.$LastName.'</div>';
    		echo "$output";	
     		
     
    		$result =	$wpdb->get_results( "SELECT ID, Title, FirstName, LastName, Address, City, State, Country,Zip, Phone, CellPhone, Email, Notes FROM HFA_Members WHERE ID = '$ID' ") ;	
    
    		while($row = mysqli_fetch_array($result))
    {
    ?>
    
    <h3>	 Member Data	 </h3>
    	<form action="" method = "POST">			
    	<input type="hidden" name ="ID" value="<?php echo  $row['ID'] ?>"/><br>
    	<input type="text" name ="Title" value="<?php echo  $row['Title'] ?>"/><br>	
    	<input type="text" name ="FirstName" value="<?php echo  $row['FirstName'] ?>"/><br>			
    	<input type="text" name ="LastName" value="<?php echo  $row['LastName'] ?>"/><br>	         
    	<input type="text" name ="Address" value="<?php echo  $row['Address'] ?>"/><br>	
    	<input type="text" name ="City" value="<?php echo  $row['City'] ?>"/><br>
    	<input type="text" name ="State" value="<?php echo  $row['State'] ?>"/><br>
    	<input type="text" name ="Country" value="<?php echo  $row['Country'] ?>"/><br>
    	<input type="text" name ="Zip" value="<?php echo  $row['Zip'] ?>"/><br>	
    	<input type="text" name ="Phone" value="<?php echo  $row['Phone'] ?>"/><br>	
    	<input type="text" name ="CellPhone" value="<?php echo  $row['CellPhone'] ?>"/><br>	
    	<input type="text" name ="Email" value="<?php echo  $row['Email'] ?>"/><br>	
    	<input type="text" name ="Notes" value="<?php echo  $row['Notes'] ?>"/><br>
    	
    	<input type="submit" name="update" value="Update Data">
    	</form>
    </center>
    </body>
    </html>
    <?php
          
    }		
    		
    ?>
    
    <?php
    if(isset($_POST['update']))
    {	$Title = $_POST['Title'];
    	$FirstName = $_POST['FirstName'];
    	$LastName = $_POST['LastName'];
    	$Address = $_POST['Address'];
    	$City = $_POST['City'];
    	$Country = $_POST['Country'];
    	$Zip = $_POST['Zip'];
    	$Phone = $_POST['Phone'];
    	$CellPhone = $_POST['CellPhone'];
    	$Email = $_POST['Email'];
    	$Notes = $_POST['Notes'];
    $query= "UPDATE 'HFA_Members' SET Title =' $_POST[Title]', FirstName =' $_POST[FirstName]', LastName = '$_POST[LastName]',Address ='$_POST[Address]', City ='$_POST[City]', State ='$_POST[State]', Country ='$_POST[Country]', Zip ='$_POST[Zip]', Phone ='$_POST[Phone]', CellPhone ='$_POST[CellPhone]', Email ='$_POST[Email]', Notes ='$_POST[Notes]' WHERE ID='$_POST[ID]' ";
    
    $query_run =mysqli_query($link,$query) ;
    
    if($query_run)
    {
    echo "Data Updated";
    }
    else {
    echo "Data not Updated";
    }
    }
    ?>
    
    
                
    	
    
    First, the page fills in the ID and first and last names of the first record in the table before search, but correctly finds the record from a first and last name. 
    But foreach($result as $row) { does not execute.  I know I'm missing something basic...

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Which foreach to you mean?

    Thread Starter hsysgrp

    (@hsysgrp)

    
    
    <form id = 2 action= "https://hawesfamilyassociation.com/wp-content/themes/twentyfourteen-child/HFA_Members_UpdateDesc.php/" method='POST'>

    <label for="FirstName">First name:</label>
    <input type="text" id="FirstName" name="FirstName" value="<?php echo $row->FirstName; ?>"><br>
    <label for="LastName">Last name:</label>
    <input type="text" id="LastName" name="LastName" value="<?php echo $row->LastName;?>"><br>
    <label for="Address">Address:</label>
    <input type="text" id="Address" name="Address" value="<?php echo $row->Address; ?>"><br>
    <label for="City">City:</label>
    <input type="text" id="City" name="City" value="<?php echo $row->City;?>"><br>
    <label for="State">State:</label>
    <input type="text" id="State" name="State" value="<?php echo $row->State; ?>"><br>
    <label for="Country">Country:</label>
    <input type="text" id="Country" name="Country" value="<?php echo $row->Country;?>"><br>
    <label for="Zip">Zip:</label>
    <input type="text" id="Zip" name="Zip" value="<?php echo $row->Zip; ?>"><br>
    <label for="Phone">Phone:</label>
    <input type="text" id="Phone" name="Phone" value="<?php echo $row->Phone;?>"><br>
    <label for="CellPhone">CellPhone:</label>
    <input type="text" id="CellPhone" name="CellPhone" value="<?php echo $row->CellPhone;?>"><br>
    <label for="Email">Email:</label>
    <input type="text" id="Email" name="Email" value="<?php echo $row->Email;?>"><br>
    <label for="Notes">Notes:</label>
    <input type="text" id="Notes" name="Notes" value="<?php echo $row->Notes; ?>"><br>

    <input type="submit" name = "submit" value="Update">
    </form>
    I narrowed it down to POST is not sending data to the update script. The script is accessed, but echo "$ID"; is null.
    Thank you for your response.
    Moderator bcworkz

    (@bcworkz)

    Hi hsysgrp,

    The query is apparently failing to find any matches.

    You should confirm if $wpdb has a valid wpdb connection object assigned to it. It may not be a proper object if your myFuncs.php inclusion does not initialize the WP environment. That file would need to include or require wp-load.php or similar to get WP loaded.

    Note that loading WP that way isn’t reliably portable to other WP installations. The preferred way to get WP loaded would be to make the template with your form one of the theme’s page templates. Relate a page post type you create to this template. Then have your form submit to that page’s permalink instead of directly to the file.

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you for your response, the problem was a coding error somewhere, never found it, rewrote and it worked.

    	if (isset($_POST['submit'])){
    	echo "submit checked";
    	$tablename = 'HFA_Members';
    	echo $tablename . "<br>";
    
    $data_update =  array(	      
    	'Title' => $Title,
       	'FirstName' => $FirstName,
    	'LastName'=> $LastName,
    	'Address'=> $Address,
    	'City'=> $City,
    	'State'=> $State,
    	'Country'=> $Country,
    	'Zip'=> $Zip,
    	'Phone'=> $Phone,
    	'CellPhone'=> $CellPhone,
    	'Email'=> $Email,
    	'Notes'=>$Notes
        
        ) ;
    $data_where =   array(    
    		'ID' => $ID  	
        );	
    $result_check = $wpdb->update($tablename, $data_update, $data_where);
    
    				
    if($result_check){
       echo "successfully updated"; 
    	 '<br/>';//successfully inserted.
    }else{
      echo "no dice"; //something gone wrong
    } //end $result_check
    
    
    mysqli_close($link);
    } //end isset
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘updating searched data’ is closed to new replies.