Form does not insert data into the database after submitted

0

Why does not the following code enter? Someone experienced can tell me the code below does not show error yet does not insert in the bank.

<form action="index.php" method="post">

addcurso:<input name="nome1" type="text">

<input name="botao1" type="submit" value="Enviar" /><br />

addinstituição:<input name="nome2" type="text">

<input name="botao2" type="submit" value="Enviar" /><br />

<?php

if (!empty($_POST['nome1']))

$nome = $_POST['nome1'];
 {
include "conecta_banco.inc";


$sql = "INSERT INTO cursos (cur_id, cur_name) Values ('$nome')";
$resultado = mysqli_query ($link,$sql);
echo "inserida";
mysqli_close($link);
}  


?>                          
    
asked by anonymous 12.11.2016 / 13:41

1 answer

1

There is a syntax error in the IF. The {} is after the variable and not after the if. I modified PHP to start the code and it worked fine.

 <?php
    if (!empty($_POST['nome1'])){

        $nome = $_POST['nome1'];


        $link = mysqli_connect("localhost", "root", "", "codeeducation");
        if ($link->connect_errno) {
            printf("Connect failed: %s\n", $link->connect_error);
            exit();
        }


        try{

            $sql = "INSERT INTO cursos (cur_name) Values ('$nome')";

            $result = $link->query($sql);

        }catch (Exception $e){
            var_dump($e);
        }


    }
        ?>

        <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="index.php" method="post">

        addcurso:<input name="nome1" type="text">

        <input name="botao1" type="submit" value="Enviar" /><br />

     </form>
    </body>
    </html>
    
12.11.2016 / 15:07