dplht39359 2017-01-05 09:21
浏览 18

以单一形式更新联合搜索中的两个表

I have two table with same column name. I need make search and update in single form for that's two tables.

Here is my code form search and fetch data (looklike working):

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!";
    }
}

and here my code to update table :

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');
}

If search result is value in table2, then i want update value in table2 only. If search result is value in table1, then i want update value in table1 only.

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

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

Code above for update not working. so please help me here. any help would be appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • dsfs1233 2017-01-06 18:13
    关注

    You can use affected_rows property as follows:

    $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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度