Give Insert in another table

Asked

Viewed 53 times

0

if (isset($_POST['menu'])) {

$menu = $_POST['menu'];

$delivery = null;
if (isset($_POST['delivery'])) {
    $delivery = $_POST['delivery'];
}
$cards = null;
if (isset($_POST['cards'])) {
    $cards = $_POST['cards'];
}
$wifi = null;
if (isset($_POST['wifi'])) {
    $wifi = $_POST['wifi'];
}
$music = null;
if (isset($_POST['music'])) {
    $music = $_POST['music'];
}
$holiday = null;
if (isset($_POST['holiday'])) {
    $holiday = $_POST['holiday'];
}
$acessible = null;
if (isset($_POST['acessible'])) {
    $acessible = $_POST['acessible'];
}
$address = null;
if (isset($_POST['address'])) {
    $address = $_POST['address'];
}

$dateFoundation = null;
if (isset($_POST['date'])) {
    $dateFoundation = $_POST['date'];
}
$cnpj = null;
if (isset($_POST['cnpj'])) {
    $cnpj = $_POST['cnpj'];
}
$latitude = null;
if (isset($_POST['latitude'])) {
    $latitude = $_POST['latitude'];
}
$longitude = null;
if (isset($_POST['longitude'])) {
    $longitude = $_POST['longitude'];
}
$link = null;
if (isset($_POST['link'])) {
    $link = $_POST['link'];
}
$site = null;
if (isset($_POST['site'])) {
    $site = $_POST['site'];
}
$facebook = null;
if (isset($_POST['facebook'])) {
    $facebook = $_POST['facebook'];
}
$instagram = null;
if (isset($_POST['instagram'])) {
    $instagram = $_POST['instagram'];
}
$googleplus = null;
if (isset($_POST['googleplus'])) {
    $googleplus = $_POST['googleplus'];
}
$pinterest = null;
if (isset($_POST['pinterest'])) {
    $pinterest = $_POST['pinterest'];
}



$stmtMoreInformations = $conn->prepare("SELECT only_delivery, card_on_delivery, wifi, live_music, 
                                        open_holiday, acessible FROM public.menu
                                        WHERE menu_id = :menu");

$stmtMoreInformations->bindValue(":menu", $menu);



if ($stmtMoreInformations->execute()) {

    $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :card,
                                                 wifi = :wifi, live_music = :music, open_holiday = :holiday, 
                                                 acessible = :acessible
                                                 WHERE menu_id = :menu");

    $stmtUpdateMoreInformations->bindValue(':menu', $menu);
    $stmtUpdateMoreInformations->bindValue(':delivery', $delivery);
    $stmtUpdateMoreInformations->bindValue(':card', $cards);
    $stmtUpdateMoreInformations->bindValue(':wifi', $wifi);
    $stmtUpdateMoreInformations->bindValue(':music', $music);
    $stmtUpdateMoreInformations->bindValue(':holiday', $holiday);
    $stmtUpdateMoreInformations->bindValue(':acessible', $acessible);
    $stmtUpdateMoreInformations->execute();
}

I have a php file with several commands of Insert and update, if you run any of them I have another table where I should give an Insert something changed. How do I assign a variable that recognizes that I must give Insert in the field such?

  • Good in this file you put you have select and update, I did not identify any Insert

  • So, but for example if I perform my update is because I changed one of my inputs, then in the other table it should appear that I changed for example $cards, the field cards. I needed to initialize a variable that received my commands. To know when I ran an update or another. In this case I have many updates.

  • Let me see if I understand, if you run the $stmtUpdateMoreInformations variable update, you want to perform another Insert in another predefined table?

  • Simmm and in my other table, I have a 'field' column, where I have to inform what was changed, if cards, if wifi, etc..

  • @Isabela you know how I can solve?

  • I’ll post an answer for you

  • Okay! Will help me a lot @Isabela

Show 2 more comments

1 answer

1


The question of running an Insert only after running the update is only to do the following:

if ($stmtMoreInformations->execute()) {

   $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :card, wifi = :wifi, live_music = :music, open_holiday = :holiday, acessible = :acessible WHERE menu_id = :menu");

   $stmtUpdateMoreInformations->bindValue(':menu', $menu);
   $stmtUpdateMoreInformations->bindValue(':delivery', $delivery);
   $stmtUpdateMoreInformations->bindValue(':card', $cards);
   $stmtUpdateMoreInformations->bindValue(':wifi', $wifi);
   $stmtUpdateMoreInformations->bindValue(':music', $music);
   $stmtUpdateMoreInformations->bindValue(':holiday', $holiday);
   $stmtUpdateMoreInformations->bindValue(':acessible', $acessible);

   if($stmtUpdateMoreInformations->execute()){
      //Executar o teu insert normalmente, pois já executou o update
   }
}

As for the question of the fields and identify which one was changed, maybe you have some better way to do it, but at the moment, I thought to make a select before giving the update where the "menu_id = :menu" and save the fields, then you compare the data, for example (I am assuming that your select has been executed and the old value of "wifi" has already been set to the variable $wifiOld:

if($stmtUpdateMoreInformations->execute()){ //Se executou o update, faça...
   if($wifi != wifiOld){ //Se o valor usado para dar update, for diferente do antigo, faça
     //Executa o que for necessário
   }
}
  • Thank you! I’ll try.

  • 1

    @Anacarolinaribeiro Remember to mark the question as the answer that best answered if she solved your problem ;)

  • Okay I’ll remember yes @Isabela!!

Browser other questions tagged

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