Write data to two tables from a PHP form

Asked

Viewed 2,838 times

2

Connection with the comic

$host = "localhost";

$user = "root";

$pass = "";

$conexao = mysqli_connect($host, $user, $pass) or die (mysql_error());

mysqli_select_db($conexao, "teste2");

HTML

<head>  
    <title>Gravar</title>
</head>
<body>
    <form type="text" method="post">
        <input type="text" name="nome">
        <input type="submit" value="enviar">
</body>

PHP

$nome = $_POST['nome'];

$sql = mysqli_query($conexao,"INSERT INTO teste1 (nome) values ('$nome')");

$sql2 = mysqli_query($conexao, "INSERT INTO teste2 (nome) values ('$nome')");

echo 'Gravado com sucesso';

Why I can’t write (name) in table teste1 and teste2?

Note: db calls same teste2

  • It stops showing error when I write any value in the database, it is as if the $nome = $_POST['nome']; when I open it’s empty, so much so that it records in the empty bank

  • Updates the form of your question.

2 answers

5


Utilize Prepared statements to avoid SQL Injection. Your question is a good example of how to use transactions and exceptions.

If the first Insert work, will be written to the database. In case the second fails, the message Gravado com sucesso will be displayed, in addition to causing confusion, the database will be with an inconsistent result.

Use transactions to inform the bank that all SQL statements must be performed successfully to write to the bank, in case one fails the others will be undone (rollback).

By turning mistakes into exceptions with mysqli_report any failure in mysqli_prepare or mysqli_stmt_execute the block catch will run, in which case will only display the error message, but other treatments can be done.

Case the two of you Inserts are successfully executed, mysqli_commit, will put the changes into effect or save the new records.

<?php
$conexao = mysqli_connect($host, $user, $pass);

mysqli_autocommit($conexao, false);
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$nome = 'teste';
$insert1 = false;
$insert2 = false;

try{
    $stmt = mysqli_prepare($conexao, "INSERT INTO teste1 values (?)");
    mysqli_stmt_bind_param($stmt, 's', $nome);
    $insert1 =  mysqli_stmt_execute($stmt);

    $stmt = mysqli_prepare($conexao, "INSERT INTO teste2 (nome) values (?)");
    mysqli_stmt_bind_param($stmt, 's', $nome);
    $insert2 = mysqli_stmt_execute($stmt);
}catch (mysqli_sql_exception $e){
   echo 'SQLState: '. $e->getCode() .' <br>Descrição: '. $e->getMessage();  
}

if($insert1 && $insert2){
    mysqli_commit($conexao);
    echo 'sucesso';
}

mysqli_close($conexao);

Recommended reading:

Mysql Transaction When? How? Why?

What is a Mysql Transaction for?

1

Put: name="Submit", send button.

And replace the php code with the bottom.

if( 'POST' == $_SERVER['REQUEST_METHOD'] && $_POST['submit'] == 'enviar' ){

    $nome = $_POST['nome'];

    $sql = mysqli_query($conexao,"INSERT INTO teste1 (nome) values ('$nome')");

    $sql2 = mysqli_query($conexao, "INSERT INTO teste2 (nome) values ('$nome')");

    echo 'Gravado com sucesso';

}

Browser other questions tagged

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