0

I am trying to add data into 3 table using PHP, atm I can only view the results of the tables that are joined .

RESULTS QUERY

  $sql = mysql_query("SELECT PART_ID, PART_DESC, SERIAL_NUM, PART.RACK_NUM, PART.PART_TYPE_ID,      PART_TYPE_DESC, LOCATION
    FROM PART
    INNER JOIN PART_TYPE ON PART.PART_TYPE_ID = PART_TYPE.PART_TYPE_ID
    INNER JOIN RACK ON RACK.RACK_NUM = PART.RACK_NUM

This will get all the rows from the PART table, and for each of the rows we find, match that row to a row in the PART_TYPE table (the condition being that they have the same PART_TYPE_ID). If no match between the PART and PART_TYPE tables can be found for a given row in the PART table, that row will not be included in the result.

My Insert Query This is where im having trouble

How do I add the data to the PART_ID, PART_TYPE and RACK tables?

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['PART_ID'])) {
    $id = mysql_real_escape_string($_POST['PART_ID']);
    $PART_DESC = mysql_real_escape_string($_POST['PART_DESC']);
    $SERIAL_NUM = mysql_real_escape_string($_POST['SERIAL_NUM']);
    $RACK_NUM = mysql_real_escape_string($_POST['RACK_NUM']);
    $PART_TYPE_ID = mysql_real_escape_string($_POST['PART_TYPE_ID']);
    $LOCATION = mysql_real_escape_string($_POST['LOCATION']);
    $PART_TYPE_DESC = mysql_real_escape_string($_POST['PART_TYPE_DESC']);
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT PART_ID FROM PART WHERE PART_ID='$id' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    **$sql = mysql_query("INSERT INTO PART (PART_ID, PART_DESC, SERIAL_NUM, RACK_NUM, PART_TYPE_ID) 
        VALUES('$id','$PART_DESC','$SERIAL_NUM','$RACK_NUM','$PART_TYPE_ID')") or die (mysql_error());**


     header("location: inventory_list.php"); 
    exit();
}
?>

enter image description here

5
  • so what is your question or where is the problem ? Commented May 30, 2011 at 17:07
  • that is such an odd way to do queries.. use PDO Commented May 30, 2011 at 17:08
  • @yes 123 is there a more effective way of doing this ? Commented May 30, 2011 at 17:24
  • 1
    yes using pdo php.net/manual/en/pdo.prepare.php Commented May 30, 2011 at 17:48
  • Outputting DB errors to non-admin users discloses too much information. Commented May 30, 2011 at 18:26

3 Answers 3

1

Micheal if I understood your problem you just need to do 2 other SQL INSERT to add data in the other table

$sql = mysql_query("INSERT INTO PART (PART_ID, PART_DESC, SERIAL_NUM, RACK_NUM, PART_TYPE_ID) 
        VALUES('$id','$PART_DESC','$SERIAL_NUM','$RACK_NUM','$PART_TYPE_ID')") or die (mysql_error());

$currentID = mysql_inserted_id();

$sql2 = mysql_query("INSERT INTO PART_TYPE [..]");
$sql3 = mysql_query("INSERT INTO RACK [..]");

You can use $currentID if you need the ID of the last record inersted into PART

But still I strongly suggest you to learn PDO http://php.net/pdo for sql

Sign up to request clarification or add additional context in comments.

4 Comments

Yes that is correct will this resolve the results not showing items that arent in the PART_TYPE table?
I don't get what's your problem
When I add items they are not listed due to the inner joins, they do get added but they select query wont displpay any new items due to my insert statement. If it do a select statement onlu no the part table the new items appear. So if I add a new item with an ID of 1001 it wont be listed only item 1003 and so on will show up using the join statement.
@michaael then your SELECT query is wrong, because FULL OUTER JOIN selects records with no foreign keys on other table
0

your table management is wrong, you never use arrows just to show that you are joining it with that table from this table, but rather from the key in first table to foreign key in the second table, that's what i would start from, maybe a better idea would be to join them using JOIN look up in google how joins are working, that may be the cause...

I agree with @yes123, that is the correct way to insert into tables, if you have a program called heidisql then use it, because there is a window to run your queries... that way to test if it is properly written also use mysql_error.

Debug, debug, and one more time debug your code.

Comments

0

Your tables are not correctly designed. Try this table structures .

In your base table Part. -

The columns in this should be:

Part_id
part_desc
serial_num

The part_type should have following columns:

part_type_id
part_type_desc
part_id -> foreign key to the parent table

The rack table should be:

Rack_num
location
part_id -> foreign key to the parent table.

So your select query to get all the part related information would be:

$sql="select * from part join part_type pt on tp.part_id=part.part_id join Rack_num rn on rn.part_id=part.part_id";

With this structure the data remains normalized. And is flexible, so if the parts are on multiple racks you just go to the rack table and add and new rack number and the part id.

2 Comments

Thanks for the answer this was for a bachelors assignment for creating an inventory site from scratch. I eventually figured it out and got an A on the assignment and the presentation. Sadly I didn't get a programming job and I am now working on my PhD, wow how time flies.
congrats on getting a A on your presentation. Dont worry about the job you will eventually get there.. keep trying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.