Jump to content

working with 2 tables


BluwAngel

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/247944-working-with-2-tables/
Share on other sites

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.