Results 1 to 9 of 9

Thread: Help plss.....

  1. #1
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default Help plss.....

    I'm having a problem in getting the value of the primary key of another table. Actually, I'm making an insert statement, but I've encountered a problem while inserting, so I make it looks like I'm only displaying what the user inputs on the previous page.... this is my tables/relation

    create database db;
    use db;

    create table department
    (deptID int not null auto_increment primary key,
    deptname varchar(40),
    location varchar(20)
    );

    create table faculty
    (facID int not null auto_increment primary key,
    facname varchar(30),
    age int,
    deptID int references department(deptID)
    );


    And this is my html code on the first page named list.html:

    <html>

    <head>

    <title>A simple HTML form</title>

    </head>

    <body>

    <form action="list2.php" method="POST">

    <p><strong>Name:</strong><br>

    <input type="text" name="name"></p>

    <p><strong>Age:</strong><br>

    <input type="text" maxlength="2" name="age"></p>

    <p><strong>Department:</strong><br>

    <input type="text" name="department"></p>


    <p><input type="submit" value="send"></p>

    </form>



    And this is my code on the next page named list2.php:


    <?php

    include('dbconnect.php');

    $name = $_POST[name];
    $age = $_POST[age];
    $department = $_POST[department];

    $id = "select * from department where deptID = '$department'";
    $query = mysql_query($id);
    $iden = mysql_fetch_array($query);


    echo "Name: " . $name . "</br>";
    echo "Age: " . $age . "</br>";
    echo "department: " . $department . "</br>";
    echo "department's ID: " . $iden[deptID];


    ?>


    By the way, here's my code on dbconnect.php:


    <?php

    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("db",$con);

    ?>



    THIS IS THE OUTPUT AFTER I FILLED UP THE FORM:

    Name: moonfang
    Age: 19
    department: math
    department's ID:




    As you can see... the value of department's ID is not displayed in the browser.... Why is that? is there something wrong on my code? Please help...

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Try putting single quotes around deptID in the $iden array.

    ie
    PHP Code:
    echo "department's ID: " $iden['deptID']; 

  3. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-01-2009)

  4. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Unhappy

    No good... Still, the value for department's ID was not displayed...

  5. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Try inserting this, it will indicate if any records are being found.

    PHP Code:
    $num=mysql_num_rows($query);
    echo 
    "number of records found = ".$num
    $x
    =0;
    while (
    $x $num) {
        
    $idmysql_result($query,$x,"ID");
    echo 
    "department's ID: " $id;
    $x++;


  6. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-01-2009)

  7. #5
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    ahw... I don't know where to put it.. also, I already tested the result when I inserted it in the database by checking out the database... When I checked the database, seems like the value of the foreign key(deptID) in employee table is zero.... but the other attributes like the name, age seems to be ok.....

  8. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This is where it goes

    PHP Code:
    <?php

    include('dbconnect.php');

    $name $_POST[name];
    $age $_POST[age];
    $department $_POST[department];

    $id "select * from department where deptID = '$department'";
    $query mysql_query($id);
    $num=mysql_num_rows($query); 
    echo 
    "number of records found = ".$num 
    $x
    =0
    while (
    $x $num) { 
        
    $idmysql_result($query,$x,"ID"); 
    echo 
    "department's ID: " $id
    $x++; 
    }  
    $iden mysql_fetch_array($query);


    echo 
    "Name: " $name "</br>";
    echo 
    "Age: " $age "</br>";
    echo 
    "department: " $department "</br>";
    echo 
    "department's ID: " $iden[deptID];


    ?>
    I also think that you need to look at the indexes of your tables, in this article it says that when you use 'references' the fields that is being referenced must be the first index in each table, http://www.unixcities.com/mysql/manual_table_types.html

  9. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-01-2009)

  10. #7
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    No good... T_T

  11. #8
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    I have had another look at the code to create this table and the deptId field is an integer.

    In your code you have a value of "math" for $department.

    You are then selecting from the table where deptId = $department, this will never return any records, you are comparing an integer with a string.

    Your statement should be

    PHP Code:
    $id "select * from department where deptname = '$department'"
    not

    PHP Code:
    $id "select * from department where deptID = '$department'"

  12. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-01-2009)

  13. #9
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Woooooooooooooowwwwww..... Thanks a million dude... Now, it works hehe... And sorry if I bothered you too much, I thought that my code is correct in syntax, and in logic, so I didn't debug it thoroughly with each line, instead, I ask immediately to someone about my problem...It's embarrassing to my part to have a minor error like this.... Hehe, I thought I wrote there the deptname, what I don't know is that I wrote deptID instead, whaaaaaat da!...... Once again dude, thanks, you taught me a lesson..... you people here are so cool, I hope, I can be one.. hehehehe....

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
  •