SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help from a smart coder.

    Hi, below is just code that pulls items out of a database. Right now it is pulling any row from the field "name." From the table called "users." From a database. How can I get a form to make some php code execute based on the name someone puts in... I can make the form field and action, have the following code for you below, just need help with the rest. So their names would be listed in the field "name."

    For instance there would be the database field named "name" with several names listed in it and data in each one of them.
    Bobby1
    Mandi1
    Ty1

    Anytime someone puts their name into a form and clicks submit, it will pull database row with their name in it plus one. If Bobby entered his name into the form field, the information in the row named bobby1 would show on the page. If Mandi put in her name in the form, the information named Mandi1 would be pulled from the database and so on.

    Database code that works for rows only.
    <?php
    $link = mysql_connect('host', 'username', 'password');

    $db_selected = mysql_select_db('database', $link);

    $query = "SELECT name FROM users";

    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result))
    {
    print "<p>$row[name]</p>";
    }

    ?>

    Hope somone can tell me how to do this, thank you very much.

  2. #2
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you would need to add a WHERE clause to your SQL statement. But it would look like this.
    PHP Code:
    <?php
    $link 
    mysql_connect('host''username''password');

    $db_selected mysql_select_db('database'$link);

    $query "SELECT name FROM users WHERE name = '"$_POST['SubmittedName'] ."'
    "
    ;

    $result mysql_query($query);

    while (
    $row mysql_fetch_array($result))
    {
    print 
    "<p>$row[name]</p>";
    }

    ?>
    That should get you started. I used the $_POST because that is what I use, if you use GET in your forms just change it to $_GET. Also be sure to protect yourself by verifying the info from the form.
    Paranoia is no longer a mental illness it is a way of life - Me

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!!! I'll try it soon.

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I looked it over well but I'm a bit confused how to hook the form up to it.
    I have table named "name" with a field of "bobby" in it. I need bobby's info to be pulled when he enters "bobby" into a form field. Thank you very much.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,

    To get the code skookum posted above you need to give the field that the user puts thier name in a name attribute of 'SubmittedName'.

    The name attribute is how you link a field to php:

    <code><input type="text" name ="submittedname"></code>

    Now whenever someone enters a name in this field the database will be checked for matches.

    Hope that helps

  6. #6
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are psychic, my father called my skookum all my life. For some reason its saying the fetch array isn't a valid argument. But it might be because I needed to alter this string.
    $query = "SELECT name WHERE name = '". $_POST['SubmittedName'] ."'
    ";

    I don't know how to say select the items listed in the actual name fields(bobby) etc. from the table called "name." You see my table name is called "name." I don't believe I need the users part. Because it goes from the table named "name" directly to the persons names like bobby etc. How to do this? Which is what I need to pull. How to do this? Thanks.


Bookmarks

Posting Permissions

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