简体   繁体   中英

PHP MySQL UPDATE only one Cell, instead of whole row

So i have a table with a row, click on 'bearbeiten', i get to a formula where i can fill in the changed name or whatever, and then everything changes instead of only the fields i wrote something in and the rest stays.

So if i would only change name and click on save, every other field in the table goes blank.

I tried it with WHERE already, and read that POST is a good method but i think i can change it with making a few changes in the $sql statement, just don't know what.

<?php
//if(isset($...)
if($_GET['aktion'] == "speichern")
{    
$ID             = $_GET['ID'];
$Anrede         = $_GET['Anrede'];
$Nachname       = $_GET['Nachname'];
$Vorname        = $_GET['Vorname'];
$Geburtsdatum   = $_GET['Geburtsdatum'];
$Telefonnummer  = $_GET['Telefonnummer'];
$Email          = $_GET['Email'];


$sql = "UPDATE Adressbuch SET Anrede = '$Anrede', Nachname = '$Nachname',Vorname = '$Vorname', Geburtsdatum = '$Geburtsdatum', Telefonnummer = '$Telefonnummer', Email = '$Email' ORDER BY ID DESC LIMIT 1";

echo '<a href="adressbuch-abfragen">Zurueck zum Adressbuch</a><br>';
require_once ('konfiguration.php');

$db_erg = mysqli_query($db_con, $sql)
    or die("Anfrage fehlgeschlagen: " . mysqli_error($db_con));

        exit;
}

How about: Just update the column, if you variable is not blank, else update it with the same value as the record already has.

UPDATE Adressbuch 
   SET Anrede =         CASE WHEN '$Anrede' != ''        THEN '$Anrede'        ELSE Anrede        END
      ,Nachname =       CASE WHEN '$Nachname' != ''      THEN '$Nachname'      ELSE Nachname      END
      ,Vorname =        CASE WHEN '$Vorname' != ''       THEN '$Vorname'       ELSE Vorname       END
      ,Geburtsdatum =   CASE WHEN '$Geburtsdatum' != ''  THEN '$Geburtsdatum'  ELSE Geburtsdatum  END
      ,Telefonnummer =  CASE WHEN '$Telefonnummer' != '' THEN '$Telefonnummer' ELSE Telefonnummer END
      ,Email =          CASE WHEN '$Email' != ''         THEN '$Email'         ELSE Email         END
 ORDER BY ID DESC LIMIT 1

Use as and see if it works. Thanks.

<?php 
require_once ('konfiguration.php');
//if(isset($...)
if($_GET['aktion'] == "speichern")
{    
  $ID             = $_GET['ID'];
  $Anrede         = $_GET['Anrede'];
  $Nachname       = $_GET['Nachname'];
  $Vorname        = $_GET['Vorname'];
  $Geburtsdatum   = $_GET['Geburtsdatum'];
  $Telefonnummer  = $_GET['Telefonnummer'];
  $Email          = $_GET['Email'];

  //use where in your query to update the particular row
  //in below query id = your column in database table
  $sql = "UPDATE Adressbuch SET Anrede = '$Anrede', Nachname = '$Nachname',Vorname = '$Vorname', Geburtsdatum = '$Geburtsdatum', Telefonnummer = '$Telefonnummer', Email = '$Email' WHERE id='$ID'";

  $db_erg = mysqli_query($db_con, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error($db_con));

  echo '<a href="adressbuch-abfragen">Zurueck zum Adressbuch</a><br>';

  exit;
}
?>

Use POST method instead of GET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM