简体   繁体   中英

PHP MySQL insert statement won't insert data into my database

I am trying to insert some data into my database with this code:

        $username = $_SESSION['user'];
        $naslov = $_POST['naslov'];//name
        $geslo = $_POST['geslo'];//password
        $vsebina = $_POST['vsebina'];//description

        if (trim($_POST['naslov'])=="" || $_POST['geslo']=="" || $_POST['vsebina']==""){
        $status = "<div class='alert-danger'>Fields are empty</div>";
           }
     else{
        $link = open_database_connection();

        echo $username;
        echo $naslov;
        echo $geslo;
        echo $vsebina;

        $sql = "INSERT INTO projects (name, password, description, username) VALUES ('$naslov','$geslo','$vsebina','$username')";
         mysqli_query($link, $sql);

        close_database_connection($link);
        $status = "<div class='alert-success'>Vic je bil dodan.</div>";
        }

The echo show the values i am putting into the forms, the SQL does not show any errors it just doesn't insert the values into the table.

check if form method is POST if its not then change the code to

     $username = $_SESSION['user'];
    $naslov = $_GET['naslov'];//name
    $geslo = $_GET['geslo'];//password
    $vsebina = $_GET['vsebina'];//description

    if (trim($_GET['naslov'])=="" || $_GET['geslo']=="" || $_GET['vsebina']==""){
    $status = "<div class='alert-danger'>Fields are empty</div>";
       }
 else{
    $link = open_database_connection();

    echo $username;
    echo $naslov;
    echo $geslo;
    echo $vsebina;

    $sql = "INSERT INTO projects (name, password, description, username) VALUES ('$naslov','$geslo','$vsebina','$username')";
     mysqli_query($link, $sql);

    close_database_connection($link);
    $status = "<div class='alert-success'>Vic je bil dodan.</div>";
    }    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM