0
votes

this is going to be my second post, I am very confused and need some assistance. first I will explain what I would want it to do and then post my code. I am first search a database using first name, last name, or a date to print the results. I first need help in only printing a repeating name once. It is printing every possible case and I would like it to match the fields (except arrival, reason, or even company (these can change ) ) Next once the results print on my search page, I would like radio buttons to be next to each set of data. for instance, if there are two people with the last name brown, i would like the first name, last name, and DL# to have ONE radio button next to it. resulting in two total buttons. this is where i am having trouble. once the selected radio is pressed and the next button (hyperlink) is pressed, i will then direct the user to a set of forms where the selected data ( firstname, lastname, dl#, company) is pre filled in the spots.

so all in all i need help limiting the prints of repeating individuals, and also i need assistance with saving data after a search function using a radio button to then print and pre populate the forms on the following page. my code currently for the search function is:

<h3>Search By First Name, Last Name, or Arrival Date (20XX-MO-DY)</h3>
<form action="searchpage.php" method="GET">
    <label>Search:
    <input type="text" name="searchname" id="searchname" />
    </label>
    <input type="submit" value="Search" />
</form>

My searchpage is :

<?php

$host = "localhost"; //server
$db = "practice_table"; //database name
$user = "root"; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$searchTerm = trim($_GET['searchname']);

// Check if $searchTerm is empty
if ($searchTerm == "") {
    echo "Enter name you are searching for.";
    exit();
} else {
    $sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL) like 
'%$searchTerm%'";
    $query = mysql_query($sql);
    $count = mysql_num_rows($query);

    if (($count) >= 1) {
        $output = "";

        while ($row = mysql_fetch_array($query)) {
            $output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
            $output .= "Last Name: " . $row['LASTNAME'] . "<br />";
            $output .= "Arrival: " . $row['ARRIVAL'] . "<br />";
        }
        echo $output;
    } else {
        echo "There was no matching record for the name " . $searchTerm;
    }
}
?>

this code above is where all the results print and I would like to regulate the repeating cases, also where the radio button per individual should be. finally this is the form page the hyperlink goes to where I would like to pre poppulate the code. Welcome Back Contractor. Please fill the following information in again below for today's visit.

First Name: 
<input type="text" name="FIRSTNAME" id="FIRSTNAME" value="<?php echo $_POST['radio']; ?>"/>
Last Name: 
<input type="text" name="LASTNAME" id="LASTNAME" value="<?php echo $_POST['radio']; ?>"/>

<form action="insert_submit.php" method="post" style="margin-left:35px;">
    First Name: <input type="text" name="FIRSTNAME" id="FIRSTNAME"/>
    Last Name: <input type="text" name="LASTNAME" id="LASTNAME"/>
    Purpose: <input type="text" name="PURPOSE" id="PURPOSE"/>
    <br />
    Company: 
    <input type="text" name="COMPANY" id="COMPANY"/>
    DL #: 
    <input type="text" name="DRIVERL" id="DRIVERL"/>
    <input type="radio" name="STATUS" id="STATUS" value="CHECKED IN">Log In
    <br/>
    <input type="radio" name="STATUS" id="STATUS" value="CHECKED OUT">Log Out
    <br/> 
    <input type="submit" value="Submit">
    <br/>
</form>

I appreciate any help. i hope I was just complicating things and can easily solve my last few issues. thank!

1
So you have duplicate names returned? How many? Is it consistent? Are you sure they don't exist in the database? I don't understand your second problem, but I don't know why you have $_POST['radio'] in there. Also those input tags are outside of the form. - Jamie Kitson
when i search the database for a specific last name for example, every case of the person prints, instead just one print. if two people have the same last name but are both in the database a few times each, i want it to print two things, not every case of each person. they do exist in the database. solely for the different arrival times. and the second problem is trying to take the results printed to the next page to pre fill the forms. this is for the different arrival time ( which is why i am resubmitting a specific person again). a radio button would help with same last names of two people - user3711444

1 Answers

0
votes

To remove duplicates from your results add DISTINCT or a GROUP BY clause to your SQL statment, eg:

SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE FROM ...

or

SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE 
FROM ... 
WHERE ... 
GROUP BY DISTINCT FIRSTNAME, LASTNAME, PURPOSE 

To add an arrival date while still only printing one row per person add MAX(ARRIVAL) to the SELECT clause:

SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE, MAX(ARRIVAL)
FROM ... 
WHERE ... 
GROUP BY DISTINCT FIRSTNAME, LASTNAME, PURPOSE