Predict next Bank ID

Asked

Viewed 479 times

0

Good staff I’m wanting to better organize the images of the posts but for that I need to know what will be the next ID so I check the image first before entering the rest of the data.

 if (is_uploaded_file($_FILES["foto"]["tmp_name"])) {
$imagem = time() . '_' . $_FILES["foto"]["name"];
$diretorio = 'fotos/ {{{AQUI VIRIA O NUMERO DO ID }}}' . $imagem;
if (!move_uploaded_file($_FILES["foto"]["tmp_name"], $diretorio)) {
  $error = TRUE;
}

I’m using Pdo for connection

  • http://answall.com/q/45676/91

  • the one where I put the id after the Insert this my doubt in question is now another is to pick up before

1 answer

1


If your id is with auto_increment just search the table information with sql below:

SHOW TABLE STATUS LIKE 'nome_tabela'

Take the value from the column Auto_increment

If you’re not with auto_increment and you are entering the id manually, just fetch the biggest id or the last registered one respectively:

SELECT MAX(id)+1 AS id FROM nome_tabela

and

SELECT id+1 AS id  FROM nome_tabela ORDER BY id DESC LIMIT 1

Just implement in your code the way you want.

I hope it helps, hugs!

  • yes more in this case then I could only get the last id inserted right ?

  • yes, just add +1, you’ll have the next one! (I edited SQL to be clearer)

  • EX: Supposing the last id is the 40 wanted to find a way to get the 41 even though it doesn’t exist yet

  • Exactly! With the current SQL I’m already adding 1 for it to bring the next id

  • It would look something like $sql = "SELECT Nid FROM posts ORDER BY id DESC LIMIT 1; $stmt = $DB->prepare($sql); $stmt->bindValue(":Cid", intval($_GET["Cid"]); $stmt->execute(); $Results = $stmt->fetchAll(); $idfuturo= ($Results[0]["NID"] + 1);

  • There you go! : D , don’t forget to change the other id: SELECT Nid FROM posts ORDER BY Nid DESC LIMIT 1

Show 1 more comment

Browser other questions tagged

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