BluwAngel Posted September 27, 2011 Share Posted September 27, 2011 hi i have problem with connecting databases hope you can help me out i have 2 tables 1. table named nastavnici - coloms:id_nastavnik(primary key, auto inc), ime_prezime 2. table named predmeti - coloms:id_predmet (primary key, auto inc), naziv, nastavnik, sati_tjedno id_nastavnik is number ime_prezime is text i created box so i get names to choose... but heres my problem how can i connect id_nastavnik (number) with 2nd table nastavnik result needs to be when i choose ime_prezime and input naziv then in table predmeti i need to get same number of ime prezime example 1. table - 1 ; John 2. table - 3(autoincrement number isnt important) , Test, 1 (THIS NUMBER IS IMPORTATNT TO BE SAME AS IN 1ST TABLE), 7 (any input number) now i created <html> <body> <form action="upis_predmeta.php" method="post"> <table> <tr><td>Predmet: <td><input name="naziv" type="text" /> <br> <tr><td>Nastavnik: <td> <?php include "spoj.php"; $result = mysql_query('SELECT * FROM nastavnici'); echo "<select name='predavac'>"; while($row = mysql_fetch_array($result)) { echo "<option>". $row['ime_prezime']. "</option>"; echo"<br>"; } echo "</select>"; ?> <tr><td>Sati tjedno (broj): <td><input name="sati_tjedno" type="text" /> <br> </table> <br> <input type="submit" /> <br><a href="index.php">Povratak</a> </form> </body> </html> and other file <?php include "spoj.php"; $SQL="INSERT INTO predmeti (naziv , sati_tjedno) VALUES ('$_POST[naziv]', '$_POST[sati_tjedno]')"; $SQL="INSERT INTO predmeti (nastavnik) FROM nastavnici VALUES ('$_POST[id_nastavnik]')"; if (mysql_query($SQL)) { echo "Novost je uspješno pohranjena"; } else { echo "Novost nije pohranjena<br />" . mysql_error(); } ?> with 2 errors (not sure its correctly since im new in this area) Notice: Undefined index: id_nastavnik in F:\xampp\htdocs\popp\upis_predmeta.php on line 8 (2nd code) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM nastavnici VALUES ('')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/ Share on other sites More sharing options...
fenway Posted September 27, 2011 Share Posted September 27, 2011 Echo the queries. Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273243 Share on other sites More sharing options...
BluwAngel Posted September 27, 2011 Author Share Posted September 27, 2011 yea but how will that help me, i still cant manage to connect them Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273278 Share on other sites More sharing options...
awjudd Posted September 28, 2011 Share Posted September 28, 2011 It will allow us to not have to piece the query together ourselves based on guesses ... if you post the actual query that is. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273397 Share on other sites More sharing options...
fenway Posted September 28, 2011 Share Posted September 28, 2011 yea but how will that help me, i still cant manage to connect them It will help *me*. Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273699 Share on other sites More sharing options...
Nodral Posted September 29, 2011 Share Posted September 29, 2011 Create a JOIN on columns 'nastavnik' on both tables, provided they reference each other then you can select from both tables dependant on you WHERE clause. As per other posts tho, very difficult to know what you want without seeing the query. Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273875 Share on other sites More sharing options...
Muddy_Funster Posted September 29, 2011 Share Posted September 29, 2011 $SQL="INSERT INTO predmeti (nastavnik) FROM nastavnici VALUES ('$_POST[id_nastavnik]')"; problem is with the above query. 1st you don't build an insert query using a FROM clause (unless you have a SELECT to go with it) 2nd you have not inserted the $_POST variable within the string properly. Try this $SQL="INSERT INTO predmeti (nastavnik) VALUES ('{$_POST['id_nastavnik']}')"; On another note, you really should not insert form varibles directly into your sql. It is a huge security risk. Quote Link to comment https://forums.phpfreaks.com/topic/247944-working-with-2-tables/#findComment-1273911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.