I am trying to insert data into databse and it doesn't work. Here is full PHP page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stil.css" />
<meta charset="UTF-8">
</head>
<body>
<div id="container">
<ul id="nav">
<li><a href="index.php">Početna</a></li>
<li><a href="">Dodaj</a></li>
<li><a href="">Dnevni unos </a></li>
</ul>
<div id="add">
<form action="dodaj.php" method="POST">
<label>Ime namirnice:</label> <input type="text" name="Ime" id="polje"/><br><br>
<label>Energetska vrednost:</label> <input type="text" name="EV" id="polje"/><br><br>
<label>Proteini:</label> <input type="text" name="Proteini"/ id="polje"><br><br>
<label>Masti:</label> <input type="text" name="Masti" id="polje"/><br><br>
<label>Ugljeni hidrati:</label> <input type="text" name="UH" id="polje"/><br><br>
<input type="submit" value="Dodaj zapis" name="dodaj" id="dugmeClick"/>
</form>
<?php
if(isset($_POST['dodaj'])) {
$link = mysqli_connect('localhost','root','','dajana');
if(!$link) {
die("Nemoguce je konektovati se na bazu.");
}
$ime = $_POST['Ime'];
$ev = $_POST['EV'];
$proteini = $_POST['Proteini'];
$masti = $_POST['Masti'];
$uh = $_POST['UH'];
$sql = "INSERT INTO Hrana('Ime','EV','Proteini','Masti','UH') VALUES('{$ime}','{$ev}','{$proteini}','{$masti}','{$uh}')";
if(mysqli_query($link,$sql)) {
"Uspesno si dodala novi zapis u tabelu.";
} else {
echo "Trenutno nije moguce dodati zapis u tabelu.";
}
mysqli_close($link);
}
?>
</div>
</div>
</body>
</html>
Now, don't pay attention because I use direct insert into table and make whole crap vulnerable to SQL injection, just pay attention if it's work (because it's for me only).
I do have database dajana, and I do have table Hrana (food). I created them before like this:
$link1 = mysqli_connect('localhost','root','','dajana');
if(!$link1) {
die("Povezivanje na bazu nije uspelo.");
}
$sql1 = "CREATE TABLE Hrana (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Ime VARCHAR(30) NOT NULL,
EV INT NOT NULL,
Proteini INT,
Masti INT,
UH INT
)";
if(mysqli_query($link1,$sql1)) {
echo "Tabela je kreirana uspesno.";
} else {
echo "Tabela nije kreirana uspesno.";
}
mysqli_close($link1);
I just don't understand why this doesn't work. Everything ends up with message
Trenutno nije moguce dodati zapis u tabelu. (At the moment it is not possible to add record into table)
It must be something with my SQL query, it MUST BE.
P.S. I didn't add here obvious part for creating whole databse as: CREATE DATABASE dajana... It's logical I have one.