SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One record search Mysql Help

    Hi

    I used PHPMyadmin to create my database of diagnostic trouble codes for cars.
    • dtc
    • definition
    • description
    • pcause
    • notes


    I am able to connect to the database and display
    everything.
    display all

    What I need to do is use a searh box to display
    one record. DTC Pxxxx

    PHP Code:
    <?php
        
    /* Connecting, selecting database */
        
    $link mysql_connect("localhost","autoclinic","password")
            or die(
    "Could not connect");
        print 
    "Connected successfully";
        
    mysql_select_db("autodata") or die("Could not select database");

        
    /* Performing SQL query */
        
    $query "SELECT * FROM dtc";
        
    $result mysql_query($query) or die("Query failed");

        
    /* Printing results in HTML */
        
    print "<table>\n";
        while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
            print 
    "\t<tr>\n";
            foreach (
    $line as $col_value) {
                print 
    "\t\t<td>$col_value</td>\n";
            }
            print 
    "\t</tr>\n";
        }
        print 
    "</table>\n";

        
    /* Free resultset */
        
    mysql_free_result($result);

        
    /* Closing connection */
        
    mysql_close($link);
    ?>
    I am lost, when I try to add a search box I get a page
    full of errors.

    I am very new to PHP and mysql

    Can anyone help

    PS does anyone know where I can get PHP learning videos.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I can help you out step by step if you like. OK, first off, you need to search on a part number, right? You need to modify your select query to include a condition. You can do so with the WHERE keyword, for example:

    SELECT * FROM Fruit WHERE type = 'apple'

    Will get all records from the Fruit table whose type field is equal to the string 'apple'.

    You need to get the part number from a HTML form, using an <input type="text" name="partNum"> field. From there, you need to incorporate that into your MySQL query as the condition for the WHERE field.

    If it sounds too hard, just post your code step by step and we will all help you out
    Get ConMan and run your own web site!

    Want free programming eBooks? http://www.devarticles.com/ebooks.php

  3. #3
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you for your help

    Ok here is what I got so far, I am not sure about my form
    PHP Code:
    <html>

    <head>
    <title>Enter DTC  Pxxxx</title>
    </head>

    <body> 
    <p>&nbsp;</p>
    <table border="1" bgcolor="white" bordercolordark="black">
        <tr>
            <td width="321" height="72">
                <form name="form1" method="post" action="result">
                    <p><input type="text" name="search" style="color:black; background-color:rgb(204,204,204);"><input type="submit" name="Submit" style="color:black; background-color:rgb(204,204,204);" value="begin Searching"></p>
                </form>
                <p><b>Enter DTC &nbsp;Pxxxx</b></p>
            </td>
        </tr>
        <tr>
            <td width="321" height="49">
                <p>&nbsp;</p>
            </td>
        </tr>
    </table>
    <p>&nbsp;</p>

     

    <?php
    $hostname 
    "localhost";          
    $username "autoclinic";      
    $password "password";          
    $usertable "dtc";          
    $dbName "autodata";          

    MYSQL_CONNECT($hostname$username$password) OR DIE("Unable to connect to database"); 
    mysql_select_db"$dbName") or die( "Unable to select database"); 



    $query mysql_query("SELECT * FROM autodata WHERE $dtc ");
    $sql_result=mysql_query($sql);


    ?> 


    </body>

    </html>
    Here is a link to that page
    Search page
    Last edited by Pete; May 16, 2002 at 23:31.

  4. #4
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help

    help

  5. #5
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Thank you for your help

    PHP Code:
    <html>

    <head>
    <title>Enter DTC  Pxxxx</title>
    </head>

    <body> 
    <p>&nbsp;</p>
    <table border="1" bgcolor="white" bordercolordark="black">
        <tr>
            <td width="321" height="72">
                <form name="form1" method="post" action="<? $_SERVER['PHP_SELF'?>">
                    <p><input type="text" name="search" style="color:black; background-color:rgb(204,204,204);"><input type="submit" name="Submit" style="color:black; background-color:rgb(204,204,204);" value="begin Searching"></p>
                </form>
                <p><b>Enter DTC &nbsp;Pxxxx</b></p>
            </td>
        </tr>
        <tr>
            <td width="321" height="49">
                <p>&nbsp;</p>
            </td>
        </tr>
    </table>
    <p>&nbsp;</p>

     

    <?php
    $hostname 
    "localhost";          
    $username "autoclinic";      
    $password "password";          
    $usertable "dtc";          
    $dbName "autodata";          

    MYSQL_CONNECT($hostname$username$password) OR DIE("Unable to connect to database"); 
    mysql_select_db"$dbName") or die( "Unable to select database"); 

    $dtc $_POST['dtc'];

    $query mysql_query("SELECT * FROM autodata WHERE $dtc ");
    $sql_result=mysql_query($sql);

    while (
    $row mysql_fetch_arry(result) ) {

    $dtc $row['dtc'];
    $definition $row['definition'];
    $description $row['description'];
    $pcause $row['pcause'];
    $notes $row['notes'];

    echo 
    $dtc "&nbsp;" $definition "&nbsp;" $description "&nbsp;" $pcause "&nbsp;" $notes;
    }

    ?> 

    <body>

    </html>
    I have re-written your code a bit, and added some stuff. I think it should work right now. If you want to know what I have done and why, feel free to ask .

  6. #6
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you Mark

    Hi

    I just tested it and still getting an error.
    Marks help with code

    the only thing I could find was array spelling,
    I have lots of questions;

    Thanks for taking the time, I know it's a pain helping
    someone who is just starting out.

  7. #7
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. I made a slight typo. Find the line in which it says: mysql_fetch_arry. If you fix that to array, it should solve the problem.

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think your problem lies with this line (which i've fixed... and this is from Mark's code):
    PHP Code:
    $query mysql_query("SELECT * FROM autodata WHERE dtc='$dtc'"); 
    your WHERE clause is incorrect.... the WHERE clause should be of the format col_name='some_value' to extract rows which have col_name having the value 'some_value'... but if $dtc is an integer variable, leave off the surrounding single quotes...

  9. #9
    SitePoint Zealot Alarion's Avatar
    Join Date
    May 2001
    Location
    Virginia
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to change
    PHP Code:
    $dtc $_POST[dtc]; 
    to
    PHP Code:
    $dtc $_POST[search]; 
    seeing as how you have no form element named "dtc" ...

    and follow redemption's advice as well.
    -=Alarion=-
    Protollix - Linux hosting from $3.95/m

  10. #10
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As slight typo? Make that a buggy script ... where was I with my head when I wrote that one?

  11. #11
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I just got back

    Still not working guys

    I hate it when I feel like a pest
    PHP Code:
    <html>

    <head>
    <title>Enter DTC  Pxxxx</title>
    </head>

    <body> 
    <p>&nbsp;</p>
    <table border="1" bgcolor="white" bordercolordark="black">
        <tr>
            <td width="321" height="72">
                <form name="form1" method="post" action="<? $_SERVER['PHP_SELF'?>">
                    <p><input type="text" name="dtc" style="color:black;
    background-color:rgb(204,204,204);"><input type="submit" name="Submit" style="color:black; background-color:rgb(204,204,204);" value="begin Searching"></p>
                </form>
                <p><b>Enter DTC &nbsp;Pxxxx</b></p>
            </td>
        </tr>
        <tr>
            <td width="321" height="49">
                <p>&nbsp;</p>
            </td>
        </tr>
    </table>
    <p>&nbsp;</p>

     

    <?php
    $hostname 
    "localhost";          
    $username "autoclinic";      
    $password "Password";          
    $usertable "dtc";          
    $dbName "autodata";          

    MYSQL_CONNECT($hostname$username$password) OR DIE("Unable to connect to database"); 
    mysql_select_db"$dbName") or die( "Unable to select database"); 

    $dtc $_POST['dtc'];

    $query mysql_query("SELECT * FROM autodata WHERE dtc='$dtc' ");
    $sql_result=mysql_query($sql);

    while (
    $row mysql_fetch_array(result) ) {

    $dtc $row['dtc'];
    $definition $row['definition'];
    $description $row['description'];
    $pcause $row['pcause'];
    $notes $row['notes'];

    echo 
    $dtc "&nbsp;" $definition "&nbsp;" $description "&nbsp;" $pcause "&nbsp;" $notes;
    }

    ?> 

    <body>

    </html>
    Still getting an error on line 43
    2 changes to Where and form
    Last edited by Pete; May 17, 2002 at 21:09.

  12. #12
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trying changing this
    PHP Code:
    $query mysql_query("SELECT * FROM autodata WHERE dtc='$dtc' "); 
    to this
    PHP Code:
    $query mysql_query("SELECT * FROM autodata WHERE dtc='$dtc'") or die('Error in Query. mySQL said ' mysql_error()); 
    and tell us what it says
    Last edited by Trav; May 17, 2002 at 15:40.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  13. #13
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit: oops double post
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  14. #14
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK

    Trav
    Here is the new error
    Error in Query. mySQL said Table 'autodata.autodata' doesn't exist


    PHP Code:
    $query mysql_query("SELECT * FROM autodata WHERE dtc='$dtc'") or die('Error in Query. mySQL said ' mysql_error()); 

  15. #15
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I don't know

    I did not mention in my ori post that
    I have one more field called dtc_id which is the
    primary key.

    I did not mention it because I don't want to display it,

    • The database name is "autodata"
    • The table name is dtc


    could the table name being the same as a field name in the table be causing a problem?

  16. #16
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok you need to change it to "SELECT * FROM dtc WHERE partname = '$dtc'" where partname is the name of the column in the dtc table where the part #s are stored.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  17. #17
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I have already done that

    First Thanks you guys

    OK

    Here is what my database looks like



    I notice in PHPMyadmin it says in the query box
    Select 'dtc' Where 1

    PHP Code:
     MYSQL_CONNECT($hostname$username$password) OR DIE("Unable to connect to database"); 
    mysql_select_db"$dbName") or die( "Unable to select database"); 

    $dtc $_POST['dtc'];

    $query mysql_query("SELECT * FROM dtc WHERE $dtc ");
    $sql_result=mysql_query($sql);

    while (
    $row mysql_fetch_array(result) ) {

    $dtc $row['dtc'];
    $definition $row['definition'];
    $description $row['description'];
    $pcause $row['pcause'];
    $notes $row['notes'];

    echo 
    $dtc "&nbsp;" $definition "&nbsp;" $description "&nbsp;" $pcause "&nbsp;" $notes;
    }

    ?> 
    Error
    Warning: Supplied argument is not a valid MySQL result resource in /home/www/autoclinic/testing/tester2.php on line 43


    Line 43 while ($row = mysql_fetch_array(result)

  18. #18
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change line 43 to
    PHP Code:
    while ($row mysql_fetch_array($query)) { 
    and change your query to
    PHP Code:
    $query mysql_query("SELECT * FROM dtc WHERE dtc = '$dtc' "); 
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  19. #19
    SitePoint Member Pete's Avatar
    Join Date
    May 2002
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    works

    Thanks everyone

    Trav that last bit of code did the trick,

    I will spend the next few days going over every line
    of code and learning what each does.

    I found an great program editor

    Maguma Studio

    It has a great help section in it.

    With all the help here I hope to get much better
    at php and mysql.

    Now I can play with the code and try to get it in
    a table.

    Thanks

    You people are great

    Final code
    PHP Code:
    <html>

    <head>
    <title>Enter DTC  Pxxxx</title>
    </head>

    <body>
    <p>&nbsp;</p>
    <table border="1" bgcolor="white" bordercolordark="black">
        <tr>
            <td width="321" height="72">
                <form name="form1" method="post" action="<? $_SERVER['PHP_SELF'?>">
                    <p><input type="text" name="dtc" style="color:black; background-color:rgb(204,204,204);"><input type="submit" name="Submit" style="color:black; background-color:rgb(204,204,204);" value="begin Searching"></p>
                </form>
                <p><b>Enter DTC &nbsp;Pxxxx</b></p>
            </td>
        </tr>
        <tr>
            <td width="321" height="49">
                <p>&nbsp;</p>
            </td>
        </tr>
    </table>
    <p>&nbsp;</p>



    <?php
    $hostname 
    "localhost";
    $username "Your username";
    $password "Your Password";
    $usertable "dtc";
    $dbName "autodata";

    MYSQL_CONNECT($hostname$username$password) OR DIE("Unable to connect to database");
    mysql_select_db"$dbName") or die( "Unable to select database");

    $dtc $_POST['dtc'];

    $query mysql_query("SELECT * FROM dtc WHERE dtc = '$dtc'") or die('Error in Query. mySQL said ' mysql_error());
    $sql_result=mysql_query($sql);

    while (
    $row mysql_fetch_array($query)) {

    $dtc $row['dtc'];
    $definition $row['definition'];
    $description $row['description'];
    $pcause $row['pcause'];
    $notes $row['notes'];

    echo 
    $dtc "&nbsp;" $definition "&nbsp;" $description "&nbsp;" $pcause "&nbsp;" $notes;
    }

    ?>

    </body>

    </html>


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
  •