I cannot insert the data into the sql table with a form

Asked

Viewed 75 times

2

I am editing a wordpress plugin, and wanted with a form that I made the user when filling the form and the data were entered in the database I already have code but something does not work, could help me?

PHP code:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pap";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$instrucao = $conn->prepare("INSERT INTO tickets(problema, eletrecidade, agua, assunto, info) VALUES(?,?,?,?,?)");
if ($instrucao == FALSE) {
    echo "<p>ERRO: dados não inseridos!</p>";
} else {
    $instrucao->bind_param("sssss", $_POST["problema"], $_POST["eletrecidade"], $_POST["agua"], $_POST["assunto"], $_POST["info"]);
    $resultado = $instrucao->execute();
    if ($resultado == TRUE) {
        echo "<p>Dados inseridos.</p>";
    } else {
        echo "<p>ERRO: dados não inseridos!</p>";
    }
}
$conn->close();
?>

HTML code:

<form name="registodados" method="POST" action="submit.php">
    <fieldset>
        <!-- Escolher problema geral -->
        <label>Problema Geral</label>
        <select name="prob" id="prob">
            <option disabled selected hidden>Escolha uma opção...</option>
            <option  value="luz">Luz</option>
            <option  value="agua">Agua</option>
            <option  value="elevador">Elevador</option>
        </select>

        <!-- Escolher problemas eletrecidade -->
        <label>Eletrecidade</label>
        <select name="eletrecidade" id="eletrecidade">
            <option disabled selected hidden>Escolha uma opção...</option>
            <option  value="curto circuito">Não há luz</option>
            <option  value="curto circuito">Curto circuito</option>
        </select>

        <!--Escolher problemas agua -->
        <label>Agua</label>
        <select name="agua" id="agua">
            <option disabled selected hidden>Escolha uma opção...</option>
            <option value="Nao ha agua">Não há água</option>
            <option value="Inundacao">Inundação</option>
        </select>
        <label for="assunto">Assunto:</label>
        <input type="text" name="assunto" id="assunto" maxlength=100 placeholder="Assunto">
    </fieldset>
    <fieldset>
        <label for="info">Info:</label>
        <textarea type="text" name="info" id="info" maxlength=50 placeholder="Descrição detalhada"></textarea>
    </fieldset>
    <div>
        <input type="reset" value="Limpar">
        <input type="submit" value="Submeter">
    </div>
</form>

And finally the SQL table:

CREATE TABLE `tickets` (
  `problema` varchar(30) NOT NULL,
  `eletrecidade` varchar(30) NOT NULL,
  `agua` varchar(30) NOT NULL,
  `assunto` varchar(30) NOT NULL,
  `info` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I’ve tried several snippets of code found on the internet and nothing works, if you can help me, it’s for a project at school I don’t have much time.

  • some error? ta going to action page? or just nothing happens?

  • nothing happens, I click on Ubmit the refresh page and nothing appears in the database

  • try like this <form name="registodados" method="POST" action="submit"> or so <form name="registodados" method="POST" action="./submit">

  • still not working, buddy

2 answers

3


In select you are named 'pro', but sending by database is 'a problem'.

Altere of: select name="prob" id="prob" --> $_POST["problema"]

To: select name="problema" id="prob" --> $_POST["problema"]

Change the name to problem or check by giving a var_dump($_POST) and see the information they are receiving and how they are passing.

0

Do the following.
Make a page with the code to insert separately, with for example the name "Insert.php"
In the form, put in the "action" this

<form name="registodados" method="POST" action="insert.php">

and on the button there at the end you put "type='Ubmit'".
Then let us know if it worked.

Browser other questions tagged

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