Add data to a MySQL table, with PHP. What is wrong?

5

Initially, I'm new to PHP and SQL, so take it easy if you have barbering in code. I'm just testing the PHP and MySQL integration, I took an example table from a book and did a quick html page to pass the parameters.

HTML: (index.html)

    <html>
     <body>
      <form action="add.php">
        <p>Nome<input name="nome" type="text"/>
        <p>Principal Ingrediente<input name="principal" type="text"/>
        <p>Quantidade<input name="quant1" type="number"/>
        <p>Ingrediente Secundário<input name="segundo" type="text"/>
        <p>Quantidade<input name="quant2" type="number"/>
        <p>Instrução<input name="instrucao" type="text"/>
        <p><input type="submit"/>
      </form>
     </body>
    </html>

PHP: (add.php)

<html>
    <body>
        <?php 
        $nome = $_GET['nome'];
        $principal = $_GET['principal'];
        $quant1 = $_GET['quant1'];
        $segundo = $_GET['segundo'];
        $quant2 = $_GET['quant2'];
        $instrucao = $_GET['instrucao'];
        $db = new mysqli('localhost', 'root', 'Another2/', 'drinks');

        $sql = "INSERT INTO drinkfaceis(nome, principal, quant1, segundo, quant2, instrucoes) 
                VALUES(?, ?, ?, ?, ?, ?);";

        $stmt = $db->prepare($sql);
        if(!$stmt){
            echo 'erro na consulta: '. $db->errno .' - '. $db->error;
        }

        echo $nome;
        echo $principal;
        echo $quant1;
        echo $segundo;
        echo $quant2;
        echo $instrucao;

        $stmt->bind_param('ssisib', $nome, $principal, $quant1, $segundo, $quant2, $instrucao);
        $stmt->execute();
        ?>
    </body>
</html>

SQL Script:

CREATE DATABASE drinks;
USE drinks;
CREATE TABLE drinkfaceis
(
    nome        VARCHAR(20),
    principal   VARCHAR(20),
    quant1      INT(10),
    segundo     VARCHAR(20),
    quant2      INT(10),
    instrucoes  BLOB
);

When testing, it does not give any error, it writes the value of the variables (which I just put to test), but when I go in phpMyAdmin and look at the table the data was not inserted.     

asked by anonymous 31.08.2015 / 04:00

1 answer

7

And once again the code was correct, the problem was autocommit disabled.

When it is off you are required to tell the database that the command (a DML insert / update / delete) must be executed permanently, this can be done in several ways through php, with commit () for success and rollback () to cancel the statement.

Recommended reading:

MySQL Transaction When? As? Why?

What is a MySQL Transaction for?

To turn on the bank autocommit with workbanch go to the menu:

Server>Option file on the General tab, look for the transaction option and check autocommit

PHPMyAdmin

    
31.08.2015 / 05:46