Contact ID not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Contact ID not working

Post by dmwesq »

I am working on an online address book. I want visitors to be able to search by first or last name, by letter, or by a field called patrol. I am hoping to have all searches on a single php page. I originally had code working with just the name fields, and then struggled to add the patrol field. Now the initial part of the search works by all 3 methods and returns the names found in the search. However, the final search by ID number which should return complete contact info does not work, and the visitor is returned to a basic search page. I have tried everything I can think of and have looked at various online tutorials and forums but no luck. Any help would be appreciated.

Code: Select all

<p>You may search either by first or last name.</p> 
	   
<form method="post" action="oabtest.php?go" id="searchform">
    <input type="text" name="name">
    <input type="submit" name="submit" value="Search">
</form>
<p><a href="?by=A">A</a> | <a href="?by=B">B</a> | <a href="?by=C">C</a> |<a href="?by=D">D</a> |<a href="?by=E">E</a> |<a href="?by=F">F</a> |<a href="?by=G">G</a> |<a href="?by=H">H</a> |<a href="?by=I">I</a> |<a href="?by=J">J</a> |<a href="?by=K">K</a> |<a href="?by=L">L</a> |<a href="?by=M">M</a> |<a href="?by=N">N</a> |<a href="?by=O">O</a> |<a href="?by=P">P</a> |<a href="?by=Q">Q</a> |<a href="?by=R">R</a> |<a href="?by=S">S</a> |<a href="?by=T">T</a> |<a href="?by=U">U</a> |<a href="?by=V">V</a> |<a href="?by=W">W</a> |<a href="?by=X">X</a> |<a href="?by=Y">Y</a> |<a href="?by=Z">Z</a> </p>

<p>You may also search by Patrol.</p>
<form method="post" action="oabtest.php?go" id="searchform">
    <input type="text" name="patrol">
    <input type="submit" name="submit" value="Search">
</form>
[syntax=php]<p>You may search either by first or last name.</p> 
	   
<form method="post" action="oabtest.php?go" id="searchform">
    <input type="text" name="name">
    <input type="submit" name="submit" value="Search">
</form>
<p><a href="?by=A">A</a> | <a href="?by=B">B</a> | <a href="?by=C">C</a> |<a href="?by=D">D</a> |<a href="?by=E">E</a> |<a href="?by=F">F</a> |<a href="?by=G">G</a> |<a href="?by=H">H</a> |<a href="?by=I">I</a> |<a href="?by=J">J</a> |<a href="?by=K">K</a> |<a href="?by=L">L</a> |<a href="?by=M">M</a> |<a href="?by=N">N</a> |<a href="?by=O">O</a> |<a href="?by=P">P</a> |<a href="?by=Q">Q</a> |<a href="?by=R">R</a> |<a href="?by=S">S</a> |<a href="?by=T">T</a> |<a href="?by=U">U</a> |<a href="?by=V">V</a> |<a href="?by=W">W</a> |<a href="?by=X">X</a> |<a href="?by=Y">Y</a> |<a href="?by=Z">Z</a> </p>

<p>You may also search by Patrol.</p>
<form method="post" action="oabtest.php?go" id="searchform">
    <input type="text" name="patrol">
    <input type="submit" name="submit" value="Search">
</form>
<?php
//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect("localhost", "*****", "*****");
if (!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db("troop97_oab");
if (!$db) {
    die("Unable to select database");
}

if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        if (preg_match("/[A-Z | a-z]+/", $_POST['name'])) {
            $name = $_POST['name'];


//-query the database table
            $sql = "SELECT ID, First_Name, Last_Name FROM contact WHERE First_Name LIKE '" . mysql_real_escape_string($name) . "%' OR Last_Name LIKE '" . mysql_real_escape_string($name) . "%'";
//-run the query against the mysql query function
            $result = mysql_query($sql);

//-count results

            $numrows = mysql_num_rows($result);

            echo "<p>" . $numrows . " results found for " . stripslashes($name) . "</p>";

//-create while loop and loop through result set
            while ($row = mysql_fetch_array($result)) {

                $First_Name = $row['First_Name'];
                $Last_Name = $row['Last_Name'];
                $ID = $row['ID'];

//-display the result of the array

                echo "<ul>\n";
                echo "<li>" . "<a href=\"oabtest.php?id=$ID\">" . $First_Name . " " . $Last_Name . "</a></li>\n";
                echo "</ul>";
            }
        } else {
            echo "<p>Please enter a search query</p>";
        }
    }
}

if (isset($_GET['by'])) {
    $letter = $_GET['by'];

//-query the database table
    $letter = mysql_real_escape_string($letter);
    $sql = "SELECT ID, First_Name, Last_Name FROM contact WHERE First_Name LIKE '" . $letter . "%' 
OR Last_Name LIKE '" . $letter . "%'";

//-run the query against the mysql query function
    $result = mysql_query($sql);

//-count results
    $numrows = mysql_num_rows($result);

    echo "<p>" . $numrows . " results found for " . $letter . "</p>";

//-create while loop and loop through result set
    while ($row = mysql_fetch_array($result)) {

        $First_Name = $row['First_Name'];
        $Last_Name = $row['Last_Name'];
        $ID = $row['ID'];

//-display the result of the array

        echo "<ul>\n";
        echo "<li>" . "<a href=\"oabtest.php?id=$ID\">" . $First_Name . " " . $Last_Name . "</a></li>\n";
        echo "</ul>";
    }
}

if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        if (preg_match("/[A-Z | a-z]+/", $_POST['patrol'])) {

            $patrol = $_POST['patrol'];



//-query the database table
            $patrol = mysql_real_escape_string($patrol);
            $sql = "SELECT ID, First_Name, Last_Name FROM contact WHERE Patrol LIKE '" . mysql_real_escape_string($patrol) . "%'";

//-run the query against the mysql query function
            $result = mysql_query($sql);

//-count results
            $numrows = mysql_num_rows($result);

            echo "<p>" . $numrows . " results found for " . $patrol . "</p>";

//-create while loop and loop through result set
            while ($row = mysql_fetch_array($result)) {

                $First_Name = $row['First_Name'];
                $Last_Name = $row['Last_Name'];
                $ID = $row['ID'];

//-display the result of the array

                echo "<ul>\n";
                echo "<li>" . "<a href=\"oabtest.php?id=$ID\">" . $First_Name . " " . $Last_Name . "</a></li>\n";
                echo "</ul>";
            }
        }

        if (isset($_GET['ID'])) {
            $contactid = $_GET['ID'];

//-query the database table
            $sql = "SELECT * FROM contact WHERE ID=" . $contactid;


//-run the query against the mysql query function
            $result = mysql_query($sql);

//-create while loop and loop through result set
            while ($row = mysql_fetch_array($result)) {

                $First_Name = $row['First_Name'];
                $Last_Name = $row['Last_Name'];
                $Home_Phone = $row['Home_Phone'];
                $Cell_Phone = $row['Cell_Phone'];
                $Work_Phone = $row['Work_Phone'];
                $Email = $row['Email'];
                $Home_Street = $row['Home_Street'];
                $Home_City = $row['Home_City'];
                $Home_State = $row['Home_State'];
                $Home_Zip = $row['Home_Zip'];
                $Troop_Role = $row['Troop_Role'];
                $Patrol = $row['Patrol'];

//-display the result of the array

                echo "<ul>\n";
                echo "<li>" . $First_Name . " " . $Last_Name . "</li>\n";
                echo (empty($Home_Phone)) ? '' : "<li>" . $Home_Phone . " Home</li>\n";
                echo (empty($Cell_Phone)) ? '' : "<li>" . $Cell_Phone . " Cell</li>\n";
                echo (empty($Work_Phone)) ? '' : "<li>" . $Work_Phone . " Work</li>\n";
                echo "<li>" . "<a href=mailto:" . $Email . ">" . $Email . "</a></li>\n";
                echo "<li>" . $Home_Street . "</li>\n";
                echo "<li>" . $Home_City . ", " . $Home_State . " " . $Home_Zip . "</li>\n";
                echo "<li>" . $Troop_Role . "</li>\n";
                echo "<li>" . $Patrol . "</li>\n";
                echo "</ul>";
            }
        }
    }
}




?>[/syntax]
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Contact ID not working

Post by social_experiment »

dmwesq wrote:However, the final search by ID number which should return complete contact info does not work
In your code you have if (isset($_GET['ID'])), which checks if the value 'ID' is in the query string but in all your forms you never call page.php?ID=. You should also note that even page.php?ID= will trigger your script so you should check that there is indeed input received.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Re: Contact ID not working

Post by dmwesq »

Can you be a bit more specific? I am trying to understand your comment but not quite getting it.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Contact ID not working

Post by social_experiment »

Code: Select all

<?php
 if (isset($_GET['ID'])) {
            $contactid = $_GET['ID'];
 // rest of the code
?>
For the code to work, you need a url that looks like this : yourpage.php?ID=4, then the value of $contactid will be 4, but your forms only calls this page --> oabtest.php?go, which means you have $_GET['go'] instead of $_GET['ID'], which is why your last part doesnt work because $_GET['ID'] is never set.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Re: Contact ID not working

Post by dmwesq »

I know this seems hard to believe, but I'm just not getting this. When I hover over any of the returned names, the url that it is calling for is yourpage.php?ID= and the ID# that corresponds to the name being selected. Shouldn't that mean that I am calling the correct url for that search? Where else would I put the additional url in the page? What bothers me is at one point I was getting the additional fields to appear and I never had an additional url in the page. Now no matter how I try this I just can't get it to work and I am beyond confused. I appreciate your patience and help.
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Re: Contact ID not working

Post by dmwesq »

Found answer on another site - apparently did not have session_start() in the page.
Post Reply