Is it possible to insert and remove at the same time on the same php page?

0

Well, at the moment I'm doing a project with html, php and mysql and I'm having a little problem now.

Going further to the subject:
Assuming I have two tables: unconfirmed and confirmed, and what I want is in the php page to get pass the unconfirmed table data to the confirmed, but when I pass the data to the table confirmed I wanted that record to disappear unconfirmed tables. (in this case the unconfirmed table is called "host").

Sorry for the way I got to try to explain, if you do not notice something, say that I try to explain myself better.

<?php

include ('ligar.php');
$sql = "select * from hospede";
$cod_hospede=$_GET['cod_hospede'];

$sql = "INSERT INTO confirmados SELECT * FROM hospede WHERE cod_hospede = '$cod_hospede'";


$result = mysqli_query($con, $sql);

if ($result)
echo "<br><br>Dados registados com sucesso!";
else
echo "Erro ao tentar registar dados na base de dados!";
?>
<br>
<a href="confirmados.php">voltar</a>
    
asked by anonymous 25.05.2018 / 13:11

2 answers

0

Yes, it is possible. Create two queries

$insert_sql = "INSERT INTO confirmados SELECT * FROM hospede WHERE cod_hospede = '$cod_hospede'";
$result1 = mysqli_query($con, $insert_sql);

$delete_sql = "DELETE from hospede WHERE cod_hospede = '$cod_hospede'";
$result2 = mysqli_query($con, $sql);

Alternatively you could use transactions with both mysqli and PDO.

Using mysqli:

<?php
$mysqli = new mysqli("127.0.0.1", "my_user", "my_password", "dbname");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
try{
   $mysqli->begin_transaction(MYSQLI_TRANS_START_READ_ONLY);

   $mysqli->query("INSERT INTO confirmados SELECT * FROM hospede WHERE cod_hospede = '$cod_hospede'");
   $mysqli->query("DELETE from hospede WHERE cod_hospede = '$cod_hospede'");

   $mysqli->commit(); 
} catch (Exception $e) {
// Exceção lançada, rollback na transação
   $db->rollback();
}
$mysqli->close();
?>
    
25.05.2018 / 15:01
0

Just now you play the code to remove from the table you want to remove

$sql2 = DELETE FROM hospede WHERE cod_hospede = '$cod_hospede'";
$result2 = mysqli_query($con, $sql2);

But take a look at Mysql Transactions, this ensures that the two SQL commands are executed, or neither one ... helps to ensure that you will not lose any data ...

    
25.05.2018 / 13:53