Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts

    search in database with restrictions

    I have this big problem. I have a contact database with 1 table in mysql, 2 groups of people can login and add contacts to the database, each group should not be able to see the contact ifo from the other group unless a share checkbox is clicked.

    Well the problem is as followed, in the database I have column called, group_id (1 or 2), when a group logs in and they want to search in the database, display the records in the fields (input text, checkboxes, memofields), I have create this query to do so, but when I click on search I don't get the result I want, it shows the result of the other group, (I double checked the group_id is set correctly), don't where its going wrong.

    Here is the query I use:
    PHP Code:
    <?php
        
    //connect  to the database 
          
    $db=mysql_connect  ("localhost""user",  "password") or die ('I cannot connect to the database  because: ' mysql_error()); 
          
    //-select  the database to use 
          
    $mydb=mysql_select_db("contact_database");
        
    //query the database table
        
    $sql="SELECT group_id, contact_name FROM contacts WHERE group_id='1' AND contact_name LIKE \"%$search%\"";
        
    // run the query against the mysql query function
        
    $result=mysql_query($sql) or die("Couldn't execute query");
        if (
    $result
        {
        
    // create while loop and loop through result set
        
    while($row mysql_fetch_array ($result)){
                  
    $contact_name =$row["contact_name"];
                  
    $group_id =$row["group_id"];          
                  
        echo 
    "$contact_name";
        echo 
    "$group_id";
        }
        }
        else {
        echo 
    "<p>Geef een zoek woord op</p>";
        }
    ?>
    Here the form search code:
    Code:
    <form id="searchcontact" method="post" action="search.php" />
                	<input type="text" class="search2" name="search"/>&nbsp;<input type="submit" name="submit" class="buttons" value="Zoek" />	
    </form>
    Please need help!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    $sql="SELECT group_id, contact_name FROM contacts WHERE group_id='1' AND contact_name LIKE \"%$search%\"";
    Don't wrap the values by quotes, if the field type is integer. Have a try by removing that.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Still same results.

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    The problem is not clear. From your query, you are searching for records that belong to group 1 only.
    When you say "it shows the result of the other group", you mean the results show records with group_id 2 ?
    could you list some sample data from the table?

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    In the table, I have 2 records at the moment. Both with different group_id.
    The query I have listed above should display the name of the contact in that record with group_id 1 and that is Henk, but in my case it shows stephan from group 1 (I double check and I have a group_id 2 for the name stephan in de database).

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    try this code..
    PHP Code:
    $sql="SELECT group_id, contact_name FROM contacts WHERE group_id='1' AND contact_name LIKE '%$search%' "
    dont use " in sql statements.. use single quotes.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •