Add values to a database by PHP

Asked

Viewed 51 times

1

I’m trying to add values to a database through PHP. I am using Phpmyadmin for both databases and also for the site in question.

At this point the code I have is the following, I present my doubts at the end:

<html>
    <head>
        <meta charset="UTF-8">

            <link rel="stylesheet" type="text/css" href="mystyle_inserir.css">
            <link rel="stylesheet" type="text/css" href="w3schoolsExample.css">

        <title>Inserir utilizador</title>
        <style>
            form label{
                display: inline-block;
                width: 100px;
                font-weight: bold;   
            }       
        </style> 
    </head>
<body>

        <?php

            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "rhumanos";
        
            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);

            // Check connection
            if (!$conn) {
                die("Conecção falhou: " . mysqli_connect_error() );
            }
            echo "Conectado";
            
            function addToDB(){

                $nome = $_POST['nome'];
                $cargo = $_POST['cargo'];
                $email = $_POST['email'];
                $localidade = $_POST['localidade'];
                $setor = $_POST['setor'];   
                $telefone = $_POST['telefone'];
                $salario = $_POST['salario'];
                $horario = $_POST['horario'];
        
                $sql = "INSERT INTO trabalhador (nome, cargo, email, localidade, setor, telefone, salario, horario) 
                VALUES ($nome, $cargo, $email, $localidade, $setor, $telefone, $salario, $horario)";

                if (mysqli_query($conn, $sql)) {
                    echo "Adicionado com sucesso";
                } else {
                    echo "Erro: " . $sql . "<br>" . mysqli_error($conn);
                }
                mysqli_close($conn);
            }    
        ?>
    
    <h2>Exemplo de inserção numa MySQL database ao usar PHP</h2>

        <br><br>

        <form action="addToDB()" method="post">
            <label for="nome">Nome: </label>                          <input type="text" name="nome" />
            <br><br>
            <label for="cargo">Cargo: </label>                        <input type="text" name="cargo" />
            <br><br>
            <label for="email">E-Mail: </label>                       <input type="text" name="email" />
            <br><br>
            <label for="localidade">Localidade: </label>              <input type="text" name="localidade" />
            <br><br>
            <label for="setor">Setor de Trabalho: </label>            <input type="text" name="setor" />
            <br><br>
            <label for="telefone">Telefone: </label>                  <input type="text" name="telefone" />
            <br><br>
            <label for="salario">Salario (/h): </label>               <input type="text" name="salario" />
            <br><br>
            <label for="horario">Horário (QT horas): </label>         <input type="text" name="horario" />
            <br><br>
            <input type="submit" value="submit">
        </form>

</body>

</html>

I’m having two problems right now...

1º I want, when clicking on my Submit button, the addToDB() function to be executed and at this moment I do not understand the process of how to do it.

2º I tried without using function and the server returned me a format error, I know it has to do with the data I put.

VARCHAR type data needs to be formatted as follows:

-> "Employee name", in quotes to be accepted

and I’m simply adding

-> data, no formatting and don’t know how to fix it.

The goal is to simply add the data I’ve put in this image in the database and I’m not getting.

Thanks in advance.

1 answer

1


1º I want, when clicking on my Submit button, the addToDB() function to be executed and at this moment I do not understand the process of how to do it.

Your code is all mixed up. The function of the PHP will not run on the client. You should create a page PHP to process the data of your form and put in the action of your form:

index php.:

<html>
    <head>
        <meta charset="UTF-8">

            <link rel="stylesheet" type="text/css" href="mystyle_inserir.css">
            <link rel="stylesheet" type="text/css" href="w3schoolsExample.css">

        <title>Inserir utilizador</title>
        <style>
            form label{
                display: inline-block;
                width: 100px;
                font-weight: bold;
            }
        </style>
    </head>
    <body>

        <h2>Exemplo de inserção numa MySQL database ao usar PHP</h2>

            <br><br>

            <form action="form.php" method="post">
                <label for="nome">Nome: </label>                          <input type="text" name="nome" />
                <br><br>
                <label for="cargo">Cargo: </label>                        <input type="text" name="cargo" />
                <br><br>
                <label for="email">E-Mail: </label>                       <input type="text" name="email" />
                <br><br>
                <label for="localidade">Localidade: </label>              <input type="text" name="localidade" />
                <br><br>
                <label for="setor">Setor de Trabalho: </label>            <input type="text" name="setor" />
                <br><br>
                <label for="telefone">Telefone: </label>                  <input type="text" name="telefone" />
                <br><br>
                <label for="salario">Salario (/h): </label>               <input type="text" name="salario" />
                <br><br>
                <label for="horario">Horário (QT horas): </label>         <input type="text" name="horario" />
                <br><br>
                <input type="submit" value="submit">
            </form>

    </body>

</html>

php form.:

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Conecção falhou: " . mysqli_connect_error() );
    }
    // echo "Conectado";

    $nome = $_POST['nome'];
    $cargo = $_POST['cargo'];
    $email = $_POST['email'];
    $localidade = $_POST['localidade'];
    $setor = $_POST['setor'];
    $telefone = $_POST['telefone'];
    $salario = $_POST['salario'];
    $horario = $_POST['horario'];

    $sql = "INSERT INTO trabalhador (nome, cargo, email, localidade, setor, telefone, salario, horario)
    VALUES ($nome, $cargo, $email, $localidade, $setor, $telefone, $salario, $horario)";

    if (mysqli_query($conn, $sql)) {
        echo "Adicionado com sucesso";
    } else {
        echo "Erro: " . $sql . "<br>" . mysqli_error($conn);
    }
    mysqli_close($conn);

?>

Data of type VARCHAR need to be formatted as follows: -> "Employee name" in quotes to be accepted

Just update your query:

// ...
$sql = "INSERT INTO trabalhador (nome, cargo, email, localidade, setor, telefone, salario, horario)
VALUES ('$nome', '$cargo', '$email', '$localidade', '$setor', '$telefone', '$salario', '$horario')";
// ...

It is worth remembering that this way your system stays BEEEM vulnerable. Best way to perform such queries (when comes data from some input) is using the so-called Prepared Statements...

Remembering: PHP is run on the server. HTML is run on the customer. This means that you cannot call a Function of PHP in the HTML...

Browser other questions tagged

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