【问题标题】:How to concatenate php variable with mysql function result in INSERT statement如何在 INSERT 语句中将 php 变量与 mysql 函数结果连接起来
【发布时间】:2017-08-15 17:47:49
【问题描述】:

有什么方法可以在插入语句中连接一个 php 变量和一个 mysql 函数结果?

我有以下代码:

$conn = new mysqli("localhost", "root", "", "facturas");

$currDate = date("Y/m/d");

$currYear = date("y");

$S = "SELECT num_factura FROM facturas WHERE num_factura";

if($currYear > date("y")) {
    $sql = "INSERT INTO facturas (anyo, num_factura) VALUES ('$currDate', '".$currYear.(1)."')";
} else {
    if($conn->query($S)->num_rows > 0) {
        $sql = "INSERT INTO facturas (anyo, num_factura) VALUES ('$currDate', (SELECT MAX(h1.num_factura)+1 FROM facturas h1))";
    } else {
        $sql = "INSERT INTO facturas (anyo, num_factura) VALUES ('$currDate', '".$currYear.(1)."')";
    }
}

我想将 $currYear 变量与 SELECT(MAX) 函数连接起来,我尝试过 '".$currYear."(SELECT MAX(h1.num_factura)+1 FROM facturas h1)' 但它不起作用我想要,将最大列 num_facturas 增加 1 并与 $currYear 变量连接。

【问题讨论】:

    标签: php mysql concatenation


    【解决方案1】:

    您需要先进行查询

    (SELECT MAX(h1.num_factura)+1 FROM facturas h1))
    

    然后您可以将结果连接到您的原始字符串。

    $sql = "INSERT INTO facturas (anyo, num_factura) VALUES ('$currDate', '$maxNumFactura')";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多