#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Posts
    40
    Rep Power
    4

    Submit button on Each rown of table to view/edit record


    Hi All,

    I have the PHP below which displays the search results of a user's query:

    PHP Code:
    <?php
        $query 
    $_GET['query']; 
        
    // gets value sent over search form
         
        
    $min_length 3;
        
    // you can set minimum length of the query if you want
         
        
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
             
            
    $query htmlspecialchars($query); 
            
    // changes characters used in html to their equivalents, for example: < to &gt;
             
            //$query = mysqli_real_escape_string($query);
            // makes sure nobody uses SQL injection
            
            
    $strsql "SELECT * FROM tblClient
                WHERE (`Telephone_Primary` LIKE '%"
    .$query."%') OR (`Telephone_Secondary` LIKE '%".$query."%') OR (`Forenames` LIKE '%".$query."%') OR (`Surname` LIKE '%".$query."%') OR (`Company` LIKE '%".$query."%') OR (`Address_Postcode` LIKE '%".$query."%') GROUP BY Surname, Client_ID ASC";
            
            
    $raw_results mysqli_query($conn,$strsql) or die(mysqli_error());
            
            
    //echo $strsql;
            
            // * means that it selects all fields, you can also write: `id`, `title`, `text`
            // articles is the name of our table
             
            // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
            // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
            // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
             
            
    if(mysqli_num_rows($raw_results) > 0){ // if one or more rows are returned do following
                
                
    echo"<table border='1'>";
                echo
    "<tr>";
                echo
    "<th>Edit</th><th>ID</th><th>Title</th><th>Forenames</th><th>Surname</th><th>Company</th><th>Address Line 1</th><th>Address Line 2</th><th>Address Line 3</th><th>Town</th><th>Postcode</th><th>Telephone Primary</th><th>Telephone Secondary</th>";
                echo
    "</tr>";      
                while(
    $results mysqli_fetch_array($raw_results)){
                
    // $results = mysqli_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
                 
    echo"<tr>";
                 echo
    "<td>";
                echo
    "<form action=\"clientframe.php\" method=\"post\" enctype=\"multipart/form-data\" target=\"frame_e\">";
                echo
    "<input type=\"hidden\" name=\"Client_ID\" value=".$results['Client_ID'].">";
                echo
    "<input type=\"submit\" value=\"Edit\">";               
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Client_ID'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Title'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Forenames'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Surname'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Company'];
                 echo
    "</td>";             
                 echo
    "<td>";
                 echo 
    $results['Address_line1'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Address_line2'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Address_line3'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Address_Town'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Address_Postcode'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Telephone_Primary'];
                 echo
    "</td>";
                 echo
    "<td>";
                 echo 
    $results['Telephone_Secondary'];
                 echo
    "</td>";
                 echo
    "</tr>";
                    
    // posts results gotten from database(title and text) you can also show id ($results['id'])
                
    }
                echo
    "</table>";
                 
            }
            else{ 
    // if there is no matching rows do following
                
    echo "No results";
            }
             
        }
        else{ 
    // if query length is less than minimum
            
    echo "Minimum length is ".$min_length;
        }
    ?>
    In the first column of the results table I have put a submit button to open the chosen record, however I am finding that, no matter what button I click on, the record that is opened (ergo, the ID that is passed...) is that of the last record of the result set, and I can't seem to see why that is, or, how to get round it...

    Any help would be appreciated.

    Many Thanks,
    Graham
    Last edited by Graham_K; November 7th, 2017 at 11:35 AM.
  2. #2
  3. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Posts
    40
    Rep Power
    4
    Excellent!

    Many Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo