单击即可插入和更新

时间:2015-06-09 06:20:43

标签: php html database

我有两个表Billing_month并在mysql中注册。我想插入Billing_month,同时我想更新注册表中的一行(Arrears)。我怎么能这样做。

PHP代码是:

<?php
    //create connection
    include("database/db.php");

    // Check connection
    if($link === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    } 


    // Insert Data

    @$month=$_POST['Bill_month'];
    @$issuedate=$_POST['Bill_issue_date'];
    @$duedate=$_POST['Bill_due_date'];
    @$surcharge =$_POST['Surcharge'];

    $sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge) values('$month','$issuedate','$duedate','$surcharge')";

   if(mysqli_query($link, $sql)){
       echo "Records added successfully.";
   } else{
       echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
   }

   // close connection
   mysqli_close($link);
?>

需要你的帮助?

4 个答案:

答案 0 :(得分:2)

如果插入成功,我只会运行查询更新。

<?php
//create connection
include("database/db.php");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Insert Data
@$month=$_POST['Bill_month'];
@$issuedate=$_POST['Bill_issue_date'];
@$duedate=$_POST['Bill_due_date'];
@$surcharge =$_POST['Surcharge'];

$sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge) 
values('$month','$issuedate','$duedate','$surcharge')";

if(mysqli_query($link, $sql)){
  $res = "Records added successfully.";

  // Update Registration Arrears query
  $sql_upadte = "Update registration ...."
  if(mysqli_query($link, $sql_update)){
    $res .= "<br>Registration record updated successfully.";
  }else{
    $res .= "<br>ERROR: Could not able to execute.<br>".$sql_update."<br>".mysqli_error($link);
  }

  echo($res);

} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

答案 1 :(得分:0)

这是一个简单的多重查询,您可以在此处查看php - multi_query

<?php

$query = "INSERT INTO billing (...) VALUES (....) ; ";
$query .= "UPDATE billing SET .... ; ";

if(!mysqli_multi_query($link,$query)){
  ...
}

否则你仍然可以做两个单独的查询并一个接一个地执行它们

答案 2 :(得分:0)

使用两个查询来插入和更新,然后执行它们

$sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge) 
            values('$month','$issuedate','$duedate','$surcharge')";

$sqlUpdate = "Place your update query here";

$insert = mysqli_query($sql);
$update = mysqli_query($sqlUpdate);

答案 3 :(得分:0)

插入结算表后,获取最后插入的ID

$id = mysql_insert_id();

然后更新您的注册表。

$query = "UPDATE registration SET .... WHERE billing_id = $id; ";
mysql_query($query);