Form does not insert data into the database after submission

Asked

Viewed 30 times

0

Why does the following code not enter? Someone experienced can tell me the code below does not present an 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);
}  


?>                          
  • I believe the problem lies on that line $sql = "INSERT INTO cursos (cur_id, cur_name) Values ('$nome')"; cur_id is a FK ? If yes, you can remove it from that line (cur_id, cur_name), if not you should add something to that line Values('$var','$nome')

  • cur_id is auto_incremented

  • So you can remove from that line cursos (cur_id, cur_name) leaving only cur_name

  • did not solve if you have any more suggestions! I still could not solve by being beginner in programming

  • $nome = $_POST['nome1']; before { is not generating a fatal error? The structure of the conditional is if ( condição ) {}. Place <?php error_reporting(E_ALL); ?> at the beginning of the file and see if it does not give you an error message.

1 answer

1

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

 <?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>
  • [...]error-free. so I thought it was weird.

  • me too, but I’ve seen people think that white screen is not a mistake. It happens. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.