2017-01-05 2 views
1

Ich habe zwei Tabellen mit gleichen Spaltennamen. Ich brauche Suche und Aktualisierung in Einzelform für zwei Tabellen.Aktualisieren Sie zwei Tabellen von Union Suche in Einzelform

Hier sucht meine Codeform und Daten holen (looklike Arbeit):

if(isset($_POST['Search'])) { 
    $NoMohon = $mysqli->real_escape_string($_POST['Nomor']); 

    //query database 
    $resultSet = $mysqli->query("SELECT * FROM table1 WHERE Nomor = '$Nomor' union SELECT * FROM table2 WHERE Nomor = '$Nomor'"); 

    if($resultSet->num_rows > 0) { 
     while($rows = $resultSet->fetch_assoc()) { 
      $Nomor = $rows['Nomor']; 
      $Name = $rows['Name']; 
      $Age = $rows['Age']; 
     } 
    } else { 
     $output = "Data Null!"; 
    } 
} 

und hier mein Code-Tabelle zu aktualisieren:

if(isset($_POST['Save'])) { 
    $Nomor = $mysqli->real_escape_string($_POST['Nomor']); 

    //query database 
    $resultSet = $mysqli->query("SELECT * FROM table1 WHERE Nomor = '$Nomor' union SELECT * FROM table2 WHERE Nomor = '$Nomor'"); 

    $Name = strtoupper($_POST['Name']); 
    $Age = strtoupper($_POST['Age']); 

    //NEED WORKING CODE HERE 
    $sql = $mysqli->query("UPDATE table1 SET Name ='{$Name}', Age ='{$Age}' where Nomor='$Nomor' union UPDATE table2 SET Name ='{$Name}', Age ='{$Age}' where Nomor='$Nomor'"); 
    header('Location: sukses.php'); 
} 

Wenn der Wert in table2 Sucher ist, ich dann Ich möchte den Wert nur in Tabelle2 aktualisieren. Wenn das Suchergebnis ein Wert in Tabelle1 ist, möchte ich den Wert nur in Tabelle1 aktualisieren.

table1 
+-------+-------------+---------+ 
| Nomor | Name  | Age | 
+-------+-------------+---------+ 
| a1 | Supar  | 25  | 
| a2 | Rambo  | 23  | 
+-------+-------------+---------+ 

table2 
+-------+-------------+---------+ 
| Nomor | Name  | Age | 
+-------+-------------+---------+ 
| b1 | Boneng  | 28  | 
| b2 | Dennyius | 32  | 
+-------+-------------+---------+ 

Code oben für Update funktioniert nicht. Also bitte hilf mir hier. jede Hilfe würde geschätzt werden. Dank

+0

Sie können nicht einfach zwei Tabellen auf einmal aktualisieren. –

+0

jede Lösung, um Suchergebnis n von zwei Tabellen zu aktualisieren? in Einzelform? – omdx

+0

Verweisen Sie einfach die erste Antwort, hoffe, es wird eine Idee geben http://StackOverflow.com/Questions/19109666/update-two-Tables-in-Single-Query-in-Mysql –

Antwort

0

können Sie affected_rows Eigenschaft wie folgt:

$mysqli->query("UPDATE table2 SET Name ='{$Name}', Age ='{$Age}' WHERE Nomor = '$Nomor'"); 
if (!$mysqli->affected_rows) { // no changes were made 
    $mysqli->query("UPDATE table1 SET Name ='{$Name}', Age ='{$Age}' WHERE Nomor = '$Nomor'"); 
} 

Documentation

Verwandte Themen