473,503 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert data if I have foreign & primary key multi tables

25 New Member
Hi everybody

actually I need your help in fixing my code. Actually I have a library system that can be applied in university or any school and I'm stucking in a page that for loan student book. I have student table, Library table, books table, usersystem table and Bookoutonloan table as I mention it below so here in this table I have 4 foreign keys from different tables as I mentioned them before so here in the php page when I want to loan student I can't do it actually it will work but it's totally wrong so I want for example the students who are registered in the student table so in the attach php webpage I enter the student ID manual and might the number that I'm entering not in the origianal table for students for example I have 100 students registered so they will have from 1 to 100 number in their ID so if I write 500 in the loan it will accept and assign the the value to Bookoutonloan table but I want to change the way either I search by student name from stu ID number or what shall I do. Please I really need your help. Thanks

this is my SQL table
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `library`.`bookoutonloan` (
  2. `borrowid` INT( 12 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  3. `stid` INT( 12 ) NOT NULL,
  4. `bookid` INT( 12 ) NOT NULL,
  5. `userid` INT( 12 ) NOT NULL,
  6. `libraryid` INT( 12 ) NOT NULL,
  7. `dateborrow` DATE NOT NULL ,
  8. `datedueforreturn` DATE NOT NULL ,
  9. `datereturned` DATE NOT NULL ,
  10. `amountoffine` INT( 12 )
  11. ) ENGINE = MYISAM ;
  12. ALTER TABLE bookoutonloan 
  13. ADD FOREIGN KEY (stid) REFERENCES studentorstaff(stid);
  14. ALTER TABLE bookoutonloan 
  15. ADD FOREIGN KEY (bookid) REFERENCES books(bookid);
  16. ALTER TABLE bookoutonloan 
  17. ADD FOREIGN KEY (userid) REFERENCES usersystem(userid);
  18. ALTER TABLE bookoutonloan 
  19. ADD FOREIGN KEY (libraryid) REFERENCES librarybranch(libraryid);
  20.  
  21.  

This is the html with php code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $submit = &$_POST["submitt2"];
  3. $studid = $_POST["studid"];
  4. $bookid = $_POST["bookid"];
  5. $userid = $_POST["userid"];
  6. $libraryid = $_POST["libraryid"];
  7. $dateofloan = $_POST["dateofloan"];
  8. $datereturn = $_POST["datereturn"];
  9. $amountfine = $_POST["amountfine"];
  10.  
  11. if ($submit)
  12. {
  13.  
  14.  $connect = mysql_connect("localhost", "root", "");
  15.  mysql_select_db("library"); 
  16.  
  17.  $queryreg = mysql_query(" INSERT INTO bookoutonloan VALUES('','$studid','$bookid','$userid', '$libraryid','$dateofloan','$datereturn','')
  18.        ");
  19.  die("Loaned has been successed");
  20.  
  21.  
  22.  }
  23.  else 
  24.  echo "please fill in <b>all</b> fields!";
  25. ?>
  26. <html>
  27. <body>
  28. <form name="form1" method="post" action="loanbook.php">
  29.   <table width="37%" border="0">
  30.     <caption>
  31.       Loan Books to students
  32.     </caption>
  33.     <tr>
  34.       <td>Student ID:</td>
  35.       <td><input name="stuid" type="text" id="stuid" /></td>
  36.     </tr>
  37.     <tr>
  38.       <td>Book ID:</td>
  39.       <td><input name="bookid" type="text" id="bookid" /></td>
  40.     </tr>
  41.     <tr>
  42.       <td>User ID:</td>
  43.       <td><input name="userid" type="text" id="userid" /></td>
  44.     </tr>
  45.     <tr>
  46.       <td>Library ID:</td>
  47.       <td><input name="libraryid" type="text" id="libraryid" /></td>
  48.     </tr>
  49.     <tr>
  50.       <td>Date of Loan:</font></strong></td>
  51.       <td><input name="dateofloan" type="text" id="dateofloan" /></td>
  52.     </tr>
  53.     <tr>
  54.       <td>Date Due return:</td>
  55.       <td><input name="datereturn" type="text" id="datereturn" /></td>
  56.     </tr>
  57.     <tr>
  58.     <td>Amount of fine:</td>
  59.       <td><input name="amountfine" type="text" />
  60.     </tr>
  61.     <tr>
  62.       <td>&nbsp;</td>
  63.       <td><input type='submit' name='submitt2' value='Register' id='submitt2'></td>
  64.     </tr>
  65.   </table>
  66. </form>
  67. </body>
  68. </html>
  69.  
  70.  
  71.  
Jan 14 '10 #1
5 7539
dgreenhouse
250 Recognized Expert Contributor
You've got a couple of issues right off the bat...

You should name the receiving columns in the query correctly
i.e. INSERT INTO bookoutonloan (stid,bookid,...) VALUES ($studid, $bookid, ...)

Plus, you can't have a blank at the beginning of the values to be inserted.
VALUES ('', '$studid'…
That’ll try to put a char into an integer column (the auto-increment column as a matter of fact)

Also, if the conditional on $submit is true and after the query is executed (when fixed), the code just dies, but I guess that's what you wanted.

Expand|Select|Wrap|Line Numbers
  1. $queryreg = mysql_query( /* your original bad query here */
  2.   "INSERT INTO bookoutonloan VALUES (
  3.    '','$studid','$bookid','$userid','$libraryid','$dateofloan','$datereturn','')"
  4. );
  5.  
  6. // Falls of the face of the earth.
  7. die("Loaned has been successed");
The code should actually redirect the visitor somewhere else and not just fall off the face of the earth.

Homework's a bear isn't it?...! :-) Do you have anyone at school to work with?

[Reminds me of writing my first program...
Add 2 + 2 on a main frame using 80 column punched cards...
With all the JCL language to setup the environment first...
Those were the days... :-)]

After you've fixed these things, thought out your logic a bit better, write some more REAL code, come back with your results.
Jan 14 '10 #2
RomeoX
25 New Member
Thanks man for your reply actually I posted the wrong code by mistake and I've just checked it now. By the way the first two quotes in insert command "" it will work fine because there is borrowid will an increment by itself so no problem it will be fine and I'm already tried it, it works fine. but my problem is I really don't know how to think for this page and as I'm in the beginning of php and I don't have anyone to work with unless you are guys my last hope to find a soulution for my code. I'll explain more actually in this page I want to loan Student a book and I have in my database that for this page I post it up that I collect all the IDs from different tables as I'm showing it here in this link http://uploadpic.org/showpic-39097/libr.png it's only simple not a complete one just to see the other tables with the primary keys.


so I have here 5 tables and I'm working now for bookoutonloan table that I have 4 foreign keys and they R IDs but the first one is borrowid is primary key for this table and I want to make this page to be easy for user either by entering student ID and get his name because you know someone students might make mistake in their ID and if I have for example 100 students so their IDs will be from 1 to 100 but I tried to make a test for this loan like entering 400 and found that it success passing and it should check the student table that if the ID really exist there or not. Same thing with book it's difficult for user to memorize the ID for all books so wither I put drop down list to see all books and also it's difficult thing to do it. Anyway hope if you can really help I really need help and I couldn't find a solution by myself please stay with me.

This is the correct PHP code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $submit = &$_POST["submitt2"];
  4.  
  5. $studid = $_POST["stuid"];
  6. $bookid = $_POST["bookid"];
  7. $userid = $_POST["userid"];
  8. $libraryid = $_POST["libraryid"];
  9. $dateofloan = $_POST["dateofloan"];
  10. $dateduereturn = $_POST["dateofloan"];
  11. $datereturned = $_POST["datereturn"];
  12. $amountfine = $_POST["amountfine"];
  13.  
  14. if ($submit)
  15. {
  16.  
  17.  $connect = mysql_connect("localhost", "root", "");
  18.  mysql_select_db("library"); 
  19.  
  20.  $queryreg = mysql_query(" INSERT INTO bookoutonloan VALUES('','$studid','$bookid','$userid', '$libraryid','$dateofloan','$dateduereturn','$datereturned','$amountfine')
  21.        ");
  22.  die("Loaned has been successed! <a href='main.php'> return to Main page</a>");
  23.  
  24.  
  25.  }
  26.  else 
  27.  echo "please fill in <b>all</b> fields!";
  28. ?>
  29. <html>
  30. <body>
  31. <form name="form1" method="post" action="looaan.php">
  32.   <table width="37%" border="0">
  33.     <caption>
  34.       Loan Books to students
  35.     </caption>
  36.     <tr>
  37.       <td>Student ID:</td>
  38.       <td><input name="stuid" type="text" id="stuid" /></td>
  39.     </tr>
  40.     <tr>
  41.       <td>Book ID:</td>
  42.       <td><input name="bookid" type="text" id="bookid" /></td>
  43.     </tr>
  44.     <tr>
  45.       <td>User ID:</td>
  46.       <td><input name="userid" type="text" id="userid" /></td>
  47.     </tr>
  48.     <tr>
  49.       <td>Library ID:</td>
  50.       <td><input name="libraryid" type="text" id="libraryid" /></td>
  51.     </tr>
  52.     <tr>
  53.       <td>Date of Loan:</font></strong></td>
  54.       <td><input name="dateofloan" type="text" id="dateofloan" /></td>
  55.     </tr>
  56.     <tr>
  57.       <td>Date Due return:</td>
  58.       <td><input name="datereturn" type="text" id="datereturn" /></td>
  59.     </tr>
  60.     <tr>
  61.     <td>Amount of fine:</td>
  62.       <td><input name="amountfine" type="text" />
  63.     </tr>
  64.     <tr>
  65.       <td>&nbsp;</td>
  66.       <td><input type='submit' name='submitt2' value='Register' id='submitt2'></td>
  67.     </tr>
  68.   </table>
  69. </form>
  70. </body>
  71. </html>
  72.  
Jan 14 '10 #3
RomeoX
25 New Member
I'm still waiting for answer
Jan 15 '10 #4
RomeoX
25 New Member
please guys I really need a help
Jan 17 '10 #5
dgreenhouse
250 Recognized Expert Contributor
Sorry for not being unavailble, but I've been really swamped... Won't have any bandwidth for another couple of days...

My one recommendation though is to rewrite your problem description(s) using short, logical, bullet points and only write a narrative (long description) when it's really warranted.

I realize that english is not your first language, but for those of us who weren't requred to study multiple languages in school, it will allow us to help you more efficiently.

I'd also recommend learning how to use 'use case modeling' and the basis of 'standard flow charting' to assist you in solidifying your application logic & design and to help clear up your thinking when it comes to your programming in general.

An excellent "Use Case Modelling" tool called "Case Complete" is from Serlio software.
http://www.casecomplete.com/. You can download it (only on Windows) and use it for 30 days for free during which time you can learn some new techniques. There are a number of free flow charting tools available on the web.

Sorry again, but I'm really burning the "midnight oil" at the moment.
Jan 17 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5088
by: Victor M. | last post by:
Hello everyone. I am a newbie to Oracle 8 so please be patient and thanks for your time. The problem is the above mentioned error. I have two databases successully created using SQL*Plus 8.03....
14
36956
by: Andre | last post by:
Hello Can anyone help me translate this from access so that it can work in mssql (i need to get next value, but cannot use identity as if row is deleted, another must get new next column number...
16
16975
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
2777
by: SuffrinMick | last post by:
Hi All I have two tables: tblRecords and tblOptions. tblRecords consists of RecordID (autonumber), Record (text) and Option (text record of options chosen) fields tbloptions consists of...
7
1934
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" +...
9
3891
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
8
3486
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
1
12037
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D,...
7
9755
by: jthep | last post by:
Hi, I'm a newbie at this but how can I populate a table with data from other tables by using INSERT statements? Below is of what I have so far, I have to populate the PERSON_PROFILE table. I've...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.