Author Topic: [PHP] Insert data into database  (Read 477 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
[PHP] Insert data into database
« on: January 20, 2015, 12:05:22 pm »
I am trying to insert data into databse and it doesn't work. Here is full PHP  page:

Code: (php) [Select]
<!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>&nbsp;<input type="text" name="Ime" id="polje"/><br><br>
                                <label>Energetska vrednost:</label>&nbsp;<input type="text" name="EV" id="polje"/><br><br>
                                <label>Proteini:</label>&nbsp;<input type="text" name="Proteini"/ id="polje"><br><br>
                                <label>Masti:</label>&nbsp;<input type="text" name="Masti" id="polje"/><br><br>
                                <label>Ugljeni hidrati:</label>&nbsp;<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:

Code: (php) [Select]
$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.
Vae Victis - suffering to the conquered

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: [PHP] Insert data into database
« Reply #1 on: January 20, 2015, 12:15:42 pm »
You are using mysqli_connect which doesnt return FALSE if it fails


please see: http://www.w3schools.com/php/func_mysqli_connect.asp to check proper usage and error checking.


Also, if I'm reading this right you are running your SQL server as root, assuming he empty password is so we don't see it that's still a BAD idea. NO matter who you think you're making it for.
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline Nortcele

  • Knight
  • **
  • Posts: 211
  • Cookies: -42
  • █+█=██
    • View Profile
Re: [PHP] Insert data into database
« Reply #2 on: January 20, 2015, 12:30:37 pm »
You are using mysqli_connect which doesnt return FALSE if it fails


please see: http://www.w3schools.com/php/func_mysqli_connect.asp to check proper usage and error checking.


Also, if I'm reading this right you are running your SQL server as root, assuming he empty password is so we don't see it that's still a BAD idea. NO matter who you think you're making it for.


hit the nail on the head
~JaySec
~LulzBlog

TAKE A COOKIE!




0100000101010011010000110100100101001001

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: [PHP] Insert data into database
« Reply #3 on: January 20, 2015, 01:43:56 pm »
And for X-thousand times: Your code is horrible structured. Why the heck do you need the actual database connection setup in your HTML main body? You can do that stuff in a separate file or in the head of the file.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [PHP] Insert data into database
« Reply #4 on: January 22, 2015, 01:21:18 pm »
Alright, thanks for answers. But I think that mysqli_connect does return true or false value , because if it's not, it wouldn't work with if statement. And when I failed to connect once, entering wrong data, the message appeared correctly , because function actually works.
Vae Victis - suffering to the conquered

Offline nrael

  • Peasant
  • *
  • Posts: 66
  • Cookies: -7
    • View Profile
Re: [PHP] Insert data into database
« Reply #5 on: January 28, 2015, 05:41:21 pm »
try with pdo statements.

http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

just follow the guide above, it's easy and fast and more secure.