Jump to content

How do i retrieve data from my table?


Hillary

Recommended Posts

i'm not sure what i'm doing wrong? can anyone help me?

 

<?php include('style.css') ?>
<html>
<body>
<center><?php include('img_header.php') ?></center>
<h5><?php include ("dbinit.php"); ?></h5>
<font size="12" face="arial">Search Results.</font><br>
<font face="arial">
<a href="entries.php">Go to the Directory.</a><br>
<a href="DBform.php">Submit a Contact.</a><br>
<a href="GETform.php">Find a Contact.</a><br>
<br>
<table>
    
<?php
if (isset($_GET['phonelist'])) {
    $query = sprintf("SELECT * FROM phonelist
                       WHERE firstName = '%%',
                       WHERE lastName = '%%',
                       WHERE email = '%%',
                       WHERE phone = '%%'");
    mysql_real_escape_string($_GET['phonelist']);
    $result = mysql_query($query, $db);
    $contacts = mysql_fetch_assoc($query);
    }

    if (isset($contacts)) { ?>
<?php include('dsptbl.php') ?>
<?php } else { ?>
<p>The contact you have searched for is not listed in this Directory.</p>
<?php }; ?>
<center><?php include('footer.php') ?></center>
  
  </body>
</html>

 

 

let me know if you need any other info from me.

Link to comment
Share on other sites

This

 

$query = sprintf("SELECT * FROM phonelist
                       WHERE firstName = '%%',
                       WHERE lastName = '%%',
                       WHERE email = '%%',
                       WHERE phone = '%%'");
    mysql_real_escape_string($_GET['phonelist']);
    $result = mysql_query($query, $db);
    $contacts = mysql_fetch_assoc($query);

 

is all wrong.

 

First, you need to get data from $_GET, escaping it as it comes.

 

$firstName = mysql_real_escape_string($_GET['firstName']);
$lastName = mysql_real_escape_string($_GET['lastName']);
$email= mysql_real_escape_string($_GET['email']);
$phone= mysql_real_escape_string($_GET['phone']);

 

then, create your query

 

$query = "SELECT * FROM phonelist WHERE firstName = $firstName'' AND lastName = '$lastName' AND email = '$email' AND phone = '$phone'";

 

then run the query

 

$result = mysql_query($query, $db) or die(mysql_error().": $query");
$contacts = mysql_fetch_assoc($result);

 

Link to comment
Share on other sites

ok i did what you said... i changed AND to OR... when i search for a contact in the directory, sometimes it shows me the right one, sometimes it doesnt.

 

how would i go about making it display all matches? if i type in my last name it returns me as the result but not my mom or dad... why not all 3 of us?

 

heres my page... check it out let me know if you can diagnose it?

 

http://eno.wilmu.edu/WIS-220-B1N01-SPRING2009/student1546/HHwis220/GETform.php

 

do you see what i mean?

i cant search by just an initial, but i can search by full name, some full name searches result in the wrong data retrieved but complete info searches are always right...

 

<?php include('style.css') ?>
<html>
<body>
<center>
<?php include('img_header.php') ?>
</center>
<?php include ("dbinit.php"); ?>
<font size="12" face="arial">Search Results.</font><br>
<font face="arial">
        <a href="entries.php">Go to the Directory.</a><br>
        <a href="DBform.php">Submit a Contact.</a><br>
        <a href="GETform.php">Find a Contact.</a><br>
<br>
<table>
    
<?php
// Bring me the data!!!
$firstName = mysql_real_escape_string($_GET['firstName']);
$lastName = mysql_real_escape_string($_GET['lastName']);
$email= mysql_real_escape_string($_GET['email']);
$phone= mysql_real_escape_string($_GET['phone']);

$query = "SELECT * FROM phonelist WHERE firstName LIKE '$firstName'
OR lastName LIKE '$lastName'
OR email LIKE '$email'
OR phone LIKE '$phone'";

$result = mysql_query($query, $db) or die(mysql_error().": $query");
$contacts = mysql_fetch_assoc($result);

    if (isset($contacts)) { ?>
<center>
<?php include('dsptbl.php') ?>
</center>
<?php } else { ?>
<p>The contact you have searched for is not listed in this Directory.</p>
<?php }; ?>
<center><?php include('footer.php') ?></center>
  
  </body>
</html>

 

Thanks again!!

Link to comment
Share on other sites

before i posted the last reply i tried inserting % in multiple different locations to be able to search using only an initial... still every time though, it never fails....

 

i search K

 

hillary is displayed

 

i search Kevin

 

errol is displayed

 

i just dont get it. there is a pattern though. anything listed in the table under the entry Errol doesnt work, they all result in Errol. is that weird? i think it is...

 

I'm going to read up on the EX2 mysql_query thing now....

 

you have been very helpful. i really appreciate it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.