Log in

View Full Version : Help plss.....



heavensgate15
05-31-2009, 05:49 AM
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...:confused:

forum_amnesiac
05-31-2009, 07:29 AM
Try putting single quotes around deptID in the $iden array.

ie

echo "department's ID: " . $iden['deptID'];

heavensgate15
05-31-2009, 08:21 AM
No good... Still, the value for department's ID was not displayed... :(

forum_amnesiac
05-31-2009, 09:39 AM
Try inserting this, it will indicate if any records are being found.


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

heavensgate15
05-31-2009, 01:23 PM
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.....

forum_amnesiac
05-31-2009, 02:58 PM
This is where it goes


<?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) {
$id= mysql_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

heavensgate15
05-31-2009, 03:31 PM
No good... T_T

forum_amnesiac
05-31-2009, 03:56 PM
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


$id = "select * from department where deptname = '$department'";

not


$id = "select * from department where deptID = '$department'";

heavensgate15
06-01-2009, 10:55 AM
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....