<?php
//Provjerava da li se korisnik vec ulogovao. Ako nije, ispisace gresku.
session_start();
if (!isset($_SESSION['korisnik'])) {
die('Za pristup ovoj stranici morate se prvo ulogovati.');
}
if ($_SESSION['korisnik'] != 'administrator') {
die('Za pristup ovoj stranici morate se prvo ulogovati kao administrator.');
}
  //Podaci o bazi podataka
    $hostname = ""; $db_username = "";  $db_password = "";  $db_name = "";
    $con = mysql_connect($hostname,$db_username,$db_password);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con);
    $sql = mysql_query("select * from sections");
    $sql_2 = mysql_query("select * from sections");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administracija aplikacije</title>
</head>

<body>
<p align="center"><font color="#000000"><strong>Administracija aplikacije</strong></font></p>
<form id="form1" name="form1" method="post" action="">
  <p align="center"><font color="#0000FF"> Registracija korisnika</font></p>
  <table width="286" height="199" border="0" align="center">
    <tr>
      <td width="95">Ime</td>
      <td width="181">
        <div align="left">
          <input type="text" name="ime" id="ime" />
        </div></td>
    </tr>
    <tr>
      <td height="31">Prezime</td>
      <td><div align="left">
        <input type="text" name="prezime" id="prezime" />
      </div></td>
    </tr>
    <tr>
      <td>Korisničko ime</td>
      <td><input type="text" name="username" id="username" /></td>
    </tr>
    <tr>
      <td>Lozinka</td>
      <td><input type="password" name="password" id="password" /></td>
    </tr>
    <tr>
      <td>Smijer</td>
      <td><select name="smjer_show" id="smjer_show">
        <?php
while($row = mysql_fetch_array($sql))
  {
    echo "<option>".$row['SectionName']."</option>";
  }
?>
      </select></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="register" id="register" value="- Registruj - " /></td>
    </tr>
  </table>
  <p>
    <label></label>
    <br>
    <?php
  if (isset($_POST['register'])){
  $ime = $_POST['ime'];
  $prezime = $_POST['prezime'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  $smjer_show = $_POST['smjer_show'];

  if (empty($ime) || empty($prezime) || empty($username) || empty($password)){
  die ("ERROR: Niste unijeli sve potrebne podatke!");
  }
  $con = mysql_connect($hostname,$db_username,$db_password);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con);
        $sql = mysql_query("select id from sections where sectionname ='".$smjer_show."'");
        $smjer = mysql_fetch_array($sql);
    $sql = "insert into students values (NULL,'".$ime."','".$prezime."','".$username."','".$password."',".$smjer['id'].")";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "Podaci uspjesno uneseni u bazu podataka!";
    }
  ?>
    <br>
  </p>
  <hr />
  <p align="center"><font color="#0000FF">Šifarnik smijerova</font></p>
  <p align="center">Unos novog smijera: 
    <input type="text" name="smjer_new" id="smjer_new" />
  </p>
  <p align="center">Sačuvaj smijer:
    <input type="submit" name="sacuvaj_smjer" id="sacuvaj_smjer" value="- Sacuvaj smijer -" />
  </p>
  <p>&nbsp;</p>
  <p>
    <label></label>
  </p>
  </p><br>
  <?php
  if (isset($_POST['unos_predmeta'])){
    $enter_predmet = $_POST['enter_predmet'];
    $smjer_select = $_POST['smjer_select'];
    if (empty($enter_predmet)){
  die ("ERROR: Niste unijeli ime predmeta!");
  }
  $con = mysql_connect($hostname,$db_username,$db_password);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con);
        $sql = mysql_query("select id from sections where sectionname ='".$smjer_select."'");
        $smjer = mysql_fetch_array($sql);
    $sql = "insert into subjects values (NULL,'".$enter_predmet."',".$smjer['id'].")";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "Podaci uspjesno uneseni u bazu podataka!";
  }
  ?>
  <br>
  <hr />
  <p align="center"><font color="#0000FF">Šifarnik predmeta</font></p>
  <p align="center">Naziv predmeta: 
    <input type="text" name="enter_predmet" id="enter_predmet" />
  </p>
  <p align="center">Smijer kojem predmet pripada: 
    <select name="smjer_select" id="smjer_select">
      <?php
    while($row = mysql_fetch_array($sql_2))
    {
    echo "<option>".$row['SectionName']."</option>";
    }
    ?>
    </select>
  </p>
  <p align="center">
    <input type="submit" name="unos_predmeta" id="unos_predmeta" value="- Sacuvaj predmet -" />
  </p>
  <p>
    <label></label>
  </p>
  <?php
  if (isset($_POST['sacuvaj_smjer'])){
  $smjer_new = $_POST['smjer_new'];
  if (empty($smjer_new)){
  die ("ERROR: Niste unijeli naziv smjera!");
  }
  $con = mysql_connect($hostname,$db_username,$db_password);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con);
    $sql = "insert into sections values (NULL,'".$smjer_new."')";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "Podaci uspjesno uneseni u bazu podataka!";
  }
  ?>
</form>
<p align="center"><a href="logout.php">LogOut </a></p>
</body>
</html>
<?php
mysql_close($con);
?>

part with sacuvaj_smjer is giving me this error

Error: Table 'a9752689_1.sections' doesn't exist

and I do have database with sections table and I have working connections, I just deleted it from this post

thanks in advance

Recommended Answers

All 2 Replies

Try out this example by putting this code into add_employee.php, this will take input using HTML Form and then it will create records into database.

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $emp_name = addslashes ($_POST['emp_name']);
   $emp_address = addslashes ($_POST['emp_address']);
}
else
{
   $emp_name = $_POST['emp_name'];
   $emp_address = $_POST['emp_address'];
}
$emp_salary = $_POST['emp_salary'];

$sql = "INSERT INTO employee ".
       "(emp_name,emp_address, emp_salary, join_date) ".
       "VALUES('$emp_name','$emp_address',$emp_salary, NOW())";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee Name</td>
<td><input name="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="emp_address" type="text" id="emp_address"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="emp_salary" type="text" id="emp_salary"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

I fixed it with correcting database, php code was students,sections,subjects and in database tables was Students... with big S

thanks for reply tho, am marking as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.