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 15 of 15
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Easy question with PHP output/sorting

    I have the user enter first name, last name, make/model, and license plate. I want to output each user's info in on a separate row. Whenever I have more than 2 users, it mix/matches all the info into like every possibility lol. What am I doing wrong?
    PHP Code:
    <?php
          $link 
    mysql_connect("xxx""xxx""xxx") or die("connect
          error:"
    .mysql_error());
          
    $db "xxx";
          
    mysql_select_db($db$link) or die("select DB error:".mysql_error());
          
    $result mysql_query("SELECT * FROM fname, lname, car, licenseplate");
          while(
    $row mysql_fetch_array($result))
          {
              echo 
    $row['lname'] .', '$row['fname'] .' - '$row['make'] .' '$row['model'] .' - '$row['licenseplate'];
            echo 
    '<br>';
          }
          
    mysql_close($link);
          
    ?>
    *make and model are attributes of car
    Last edited by Derek Zoolander; 08-07-2007 at 06:24 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Do you really have four different tables called fname, lname, car, and licenseplate?

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    lol, just realized I pasted the wrong code. I changed to name with attributes fname and lname. Car with attributes make and model. Then License plate.
    I'm a beginner just trying to learn.

    here you go:
    PHP Code:
    <?php
          $link 
    mysql_connect("xxx""xxx""xxx") or die("connect
          error:"
    .mysql_error());
          
    $db "xxx";
          
    mysql_select_db($db$link) or die("select DB error:".mysql_error());
          
    $result mysql_query("SELECT * FROM name, car, licenseplate");
          while(
    $row mysql_fetch_array($result))
          {
              echo 
    $row['fname'] .', '$row['lname'] .' - '$row['make'] .' '$row['model'] .' - '$row['licenseplate'];
            echo 
    '<br>';
          }
          
          
    mysql_close($link);
          
    ?>
    Last edited by Derek Zoolander; 08-07-2007 at 06:34 AM.

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    I suspect that your tables are not properly "normalized". It would probably be a good idea to post your table structure, and get some advice on that before proceeding. It will likely make your life easier now and down the road with this project.

  • #5
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm not worried about normalization for now. This is just for fun.

    Tables:
    name - attributes: lname, fname
    car - attributes: make, model
    licenseplate - attributes: licenseplate (primary key)

    I want to print all of this data on 1 line for each user, and sort by lname or licenseplate.

  • #6
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm not worried about normalization for now. This is just for fun.

    Tables:
    name - attributes: lname, fname
    car - attributes: make, model
    licenseplate - attributes: licenseplate (primary key)

    I want to print all of this data on 1 line for each user, and sort by lname or licenseplate.

  • #7
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Well for fun or not, the data still needs to be normalized in order to obtain the results you want.

    Since a license plate can only be associated with one car, I see no need in keeping that in a separate table. Also, you will need some way of associating the car information with the individual information.

    I'd suggest a table structure like this:

    name table:
    Code:
    id - primary key, int, auto-increment
    lname - varchar of appropriate size
    fname - varchar of appropriate size
    car table
    Code:
    id - primary key, int, auto-increment
    name_id - int
    make - varchar of appropriate size
    model - varchar of appropriate size
    licenseplate - varchar of appropriate size
    Now, when a user inputs their information, they'll be assigned an id for the name table automatically. You can use this value as the name_id in the car table.

    Now, you can associate a car with a user by the name_id field. You'll also be able to sort your results as you wish. You can create keys on the lname field in the name table and the licenseplate field in the car table.

    Of course, if your only allowing users to have one car, you can keep it all in one table.

  • #8
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Alright, sometimes I have trouble thinking of it that way. I'll try it out. Users can only have 1 car, so could I do?:

    Name table:
    id
    lname
    fname
    make
    model
    licenseplate

  • #9
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    yep, that would work.

  • #10
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok, so how would i write the output? I don't get what the point of having an id is. Can't I just output $row['fname'] .' '. $row['lname'] etc.?

  • #11
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    The id was needed when you were using multiple tables - it was used to associate records from 1 table to the related records in another table.

    Since you've switched to 1 table you can ignore it in your output.

  • #12
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Right.
    Ok, so this is what i've got. It's outputting the correct info that is already in the database, but now the form submit isn't working to populate the database. Here's the code:

    <form method="POST" action=register.php>
    First Name: <input type="text" name="fname"><br><br>
    Last Name: <input type="text" name="lname"><br><br>
    Make: <input type="text" name="make"><br><br>
    Model: <input type="text" name="model"><br><br>
    State: <input type="text" name="state"><br><br>
    License Plate: <input type="text" name="licenseplate"><br><br>
    <input type=submit value="Submit">
    </form>

    PHP Code:
    <?php
    $link 
    mysql_connect("xxx""xxx""xxx") or die("connect
    error:"
    .mysql_error());
    $db "xxx";
    mysql_select_db($db$link) or die("select DB error:".mysql_error());
    $fname $_REQUEST['fname'];
    $lname $_REQUEST['lname'];
    $make $_REQUEST['make'];
    $model $_REQUEST['model'];
    $licenseplate $_REQUEST['licenseplate'];
    $result mysql_query("INSERT INTO name VALUES ('$fname', '$lname', '$make', '$model', '$licenseplate')");
    print 
    "<br><br>Your information has been entered.<br> Redirecting...";
    mysql_close($link);
    ?>

  • #13
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    If you don't specify field names in your INSERT, then you must supply values for all fields.

    Also, always add error checking to your db operations, it can help point out problems for you.
    PHP Code:
    $result mysql_query("INSERT INTO name VALUES ('$fname', '$lname', '$make', '$model', '$licenseplate')") or die(mysql_error()); 

  • #14
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Do you mean it should be something like this? I'm kinda confused now, because I thought I was doing it right:
    PHP Code:
    $result mysql_query("INSERT '$fname', '$lname', '$make', '$model', '$state', '$licenseplate' INTO name VALUES ('$fname', '$lname', '$make', '$model', '$state', '$licenseplate')") or die(mysql_error()); 
    Thanks for bearing with me.
    Last edited by Derek Zoolander; 08-09-2007 at 11:49 PM.

  • #15
    New Coder
    Join Date
    Apr 2007
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Haha, I fixed it. I was missing a ' and something else. Thanks for all your help. I really appreciate it


  •  

    Posting Permissions

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