How to select a field with MAX()+1 and use in an INSERT?

Asked

Viewed 101 times

1

I have the following code:

$Position = $this->conn->prepare("SELECT MAX(OrderTask)+1 as OrderNew FROM tasks");
        $Position->execute();       
        $newPosition = $Position->fetchAll(PDO::FETCH_NUM);
        $newPosition = $row['OrderNew'];

        $stmt = $this->conn->prepare("INSERT INTO tasks (Project, CompanyFantasy, Priorities, Delivery, Attachment, ByUser, Systems, OrderTask, Subject) VALUES 
                                    (:project, :companyfantasy, :priorities, :delivery, :attachment, :byuser, :systems, :ordertask, :subject)");

        $stmt->bindparam(":project", $project);         
        $stmt->bindparam(":companyfantasy", $companyfantasy);
        $stmt->bindParam(":priorities", $priorities);
        $stmt->bindParam(":delivery", $delivery);           
        $stmt->bindparam(":attachment", $file_name);
        $stmt->bindParam(":byuser", $byuser);           
        $stmt->bindParam(":systems", $systems);
        $stmt->bindParam(":ordertask", $newPosition);
        $stmt->bindParam(":subject", $subject);         
        $stmt->execute();

The $Position: Does the SELECT of the line OrderTask, inside the table Tasks, where you take the last number, add +1 and write to Ordernew.

The $newPosition : Holds the value of $Position.

But when I try to play the return of $newPosition within the INSERT this value is always 'NULL'.

Someone has an idea of how to take the value of OrderTask and add +1?

2 answers

2

fetchAll() Returns an array in the following structure: [0] => array(OrderNew => 10) to access it is necessary to specify the Intel zero $row[0]['OrderNew'];

As the return is always a change line fetchAll() for fetch() which returns only one line, so the access can be 'direct' ex: $row['OrderNew'];

  • I didn’t notice, but I did fetchAll() for testing, but same as fetch() always returns zero.

  • @Wagnervian you are using FETCH_ASSOC or FETCH_NUM? if it is the second just change $row['OrderNew'] for $row[0]

  • I’m using FETCH_NUM and have already put FETCH_ASSOC and the two return NULL, even putting $row['OrderNew] or $row[0].

  • @Wagnervian do print_r($Position->fetchAll(PDO::FETCH_NUM)); and put here the result.

  • Only Array()

2


You can pass your select inside your Insert,

 $stmt = $this->conn->prepare("INSERT INTO tasks (Project, CompanyFantasy, Priorities, Delivery, Attachment, ByUser, Systems, OrderTask, Subject) VALUES 
                                    (:project, :companyfantasy, :priorities, :delivery, :attachment, :byuser, :systems, (select OrderTask from (SELECT MAX(OrderTask)+1 as OrderTask FROM tasks) X), :subject)");

and no longer need to pass the parameter

 $stmt->bindParam(":ordertask", $newPosition);
  • 1

    It presents the following error Erro: SQLSTATE[HY000]: General error: 1093 You can't specify target table 'tasks' for update in FROM clause

  • I made a correction.

  • Thank you worked out that way.

  • You can explain why the X at the end of the select?

  • 1

    the Mysql does not accept changes from the same table in a single instruction, but accepts a temporary table from the same table, i.e., I made a select sub of your table and rename it as X could be avocado.

  • Table or field?

  • 1

    Table, you cannot use the same table to change or include a field from the same table.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.