php - mysqli语句中的@变量

标签 php mysql mysqli

我现在坐了几个小时并尝试通过我的 PHP 脚本传递多个查询。

基本上,我想查询地址文件夹中最后插入的索引,然后将该索引保存为变量“@address”,以便我可以在下一个查询等中使用它。

SQL 脚本工作正常,但是,一旦我在 PHP 脚本中使用它,它就不会插入任何新数据。

此外,我不想将其拆分为多个单个查询,因为此脚本将在 Web 应用程序中使用,其中将处理大量新注册,因此重要的是,当两个客户在同时保证外键的正确分配。

如有任何建议,我们将不胜感激。

<?php
$link = mysqli_connect("localhost", "root", "test", "test");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = 'SET @address = (select max(idaddress) from test.address) + 1; SET @customer= (select max(idcustomer) from test.customer) + 1;
        SET @contract= (select max(idcontact) from test.contract) + 1;
        INSERT INTO `test`.`address` (`idaddress`, `street`, `number`, `zip`) VALUES (@address, "main street", "584", "54545");
        INSERT INTO `test`.`customer` (`idcustomer`, `name`, `surname`, `customeraddress`) VALUES (@customer, "Smith", "Michael", @address);
        INSERT INTO `test`.`contract` (`idcontract`, `customernumber`) VALUES (@contract, @costumer);
       ';
echo $query; // for testing purposes
if (mysqli_multi_query($link, $query)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link)) {
            mysqli_free_result($result);
        }

    } while (mysqli_next_result($link));
}

mysqli_close($link);
?>

复杂性

最佳答案

用这个代替你的。希望它对你有用。

$query = "SET @address = (select max(idaddress) from test.address) + 1"; 
$query = "SET @customer= (select max(idcustomer) from test.customer) + 1";
$query = "SET @contract= (select max(idcontact) from test.contract) + 1";
$query = "INSERT INTO `test`.`address` (`idaddress`, `street`, `number`, `zip`) VALUES (@address, 'main street', "584", "54545")";
$query = "INSERT INTO `test`.`customer` (`idcustomer`, `name`, `surname`, `customeraddress`) VALUES (@customer, 'Smith', 'Michael', @address)";
$query = "INSERT INTO `test`.`contract` (`idcontract`, `customernumber`) VALUES (@contract, @costumer)";

关于php - mysqli语句中的@变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233094/

相关文章:

javascript - 在动态输入字段中显示多行

php 没有更新我的数据库

PHP 重写 AJAX post 请求

php - 在完成从mysql 到PDO 的迁移之前,我应该采取哪些安全措施?

php - 更新查询中的两列

php - 将 2 个不同的查询合并为一个

php - 使用 mysqli 中的准备语句检索行结果

php - 使用多个输入字段在 php 和 mysql 中上传多个文件

MySQL 使用现有表创建函数语句

php - 函数不工作 : PHP Fatal error: Call to a member function bind_param() on boolean