recover the record id in the table

Asked

Viewed 55 times

1

I have no idea how to recover my table id without going through the form

I have the following form

NEWS

<form method="post" autocomplete="off">

    <div class="input">
        <label>nome<strong>*</strong></label>
        <input type="text" name="nome"/>
    </div>

    <div class="input">
        <label>tags<strong>*</strong></label>
        <input type="text" name="tags"/>
    </div>

    <div class="btn">
        <input type="hidden" name="postar" value="postou"/>
        <input type="submit" value="próximo passo"/>
    </div>

</form>

PHP

if(isset($_POST['postar'])&&$_POST['postar']=='postou'){
    var_dump($_POST);
    //verifico se a noticia ja foi postada
    $vDup=$conn->prepare("SELECT id,nome FROM `noticias` WHERE `nome` = '".$_POST['nome']."'");

    $vDup->execute();

    $count=$vDup->rowCount($_POST['nome']);

    if($count>=1){
        echo "<span class='msg erro'>Essa notícia já foi publicada.</span>";
    }else{
        if(empty($_POST['nome'])){
            echo "O nome da notícia não pode ficar vazia.";
        }else{

            //modificação
            $pID=$vDup->fetchAll(PDO::FETCH_OBJ);
            foreach($pID AS $listID){
            $id=$listID->id;

            $id=$id;

            $nome=trim($_POST['nome']);
            $slug=$_POST['nome'];
            $tags=trim($_POST['tags']);

            $insertNoticia=$conn->prepare("INSERT INTO `noticias` (nome,slug,tags) VALUES ('$nome','$slug','$tags')");

            $insertNoticia->execute();

            if($insertNoticia){
                //preciso recuperar o id da minha notícia nessa url
                header("refresh: 3;painel.php?p=escrever-noticia&chave=".$_SESSION['id']."&autor=".$_SESSION['login']."&slug=".$slug."&id=".$id."");
                echo "<span class='msg sucesso'>Processando, aguarde...</span>";
            }else{
                echo "<span class='msg erro'>Erro ao postar essa notícia.</span>";
            }
          }//fecha o foreach
        }
    }
}

http://www.meupainel.com/painel.php?p=escrever-noticia&chave=2&autor=admin&slug=noticia-teste&id=1

my problem is in recovering this id , as I do not pass via post or get, I do not know how to recover it, this other page that the form sends when the news and posted, refers to another table, what I need and recover the id of the table news, to link between the two

  • Bring the ID together in your SELECT from the news table.

  • @Guilhermerigotti I edited the question with which you spoke, but now it is not registering nor giving any error, more the logic is the same that you spoke, however I am wrong in something

  • you have several options, like Sesssion $_SESSION, or you can also pass the id to html: example: <input type="hidden" id="custId" name="custId" value="3487">

  • @Mayron Da um var_dump on the return of your select to see what is coming.

  • @Bulfaitelo as well take for $_SESSION a $_SESSION that I use and my login, this way <input type="Hidden" id="custId" name="custId" value="3487"> did not understand how it works

  • @Guilhermerigotti edited the question with the var_dump even so it does not return anything, other thing, that SELECT I use to know if the news has already been posted, it may be that this the problem that is not pulling the id? SELECT id, nome FROM noticias WHERE nome = '".$_POST['name']." '` note that WHERE will pull if it matches the right name

  • @Mayron rotates the var_dump($_POST); right in the first line before this post validation, and post the result.

  • @Bulfaitelo array(8) { ["name"]=> string(1) "test" ["tags"]=> string(5) "tag 1" ["post"]=> string(6) "posted" }

  • @Mayron now puts this vara_sump inside the if just to see if he’s getting inside it.

  • @Bulfaitelo is entering yes, returned the same thing

  • @Bulfaitelo I think the problem is that SELECT, because it will only run if there is any news with the same name that I post, would it be better if I do another SELECT? but it is strange that he is not recording the news

  • @Mayron what is the result of select wheel one var_dump($vDup->execute()); and tell me the return

  • @Bulfaitelo gave this bool(true)

Show 9 more comments

1 answer

2


Checking your code you are rewriting the id, what to take, but as it does not exist yet, the correct would be to enter in the bank to get the id of the last registration.

if(isset($_POST['postar'])&&$_POST['postar']=='postou'){
    var_dump($_POST);
    //verifico se a noticia ja foi postada
    $vDup=$conn->prepare("SELECT id,nome FROM `noticias` WHERE `nome` = '".$_POST['nome']."'");

    $vDup->execute();

    $count=$vDup->rowCount();

    if($count>=1){
        echo "<span class='msg erro'>Essa notícia já foi publicada.</span>";
    }else{
        if(empty($_POST['nome'])){
            echo "O nome da notícia não pode ficar vazia.";
        }else{

            $nome=trim($_POST['nome']);
            $slug=$_POST['nome'];
            $tags=trim($_POST['tags']);

            $insertNoticia=$conn->prepare("INSERT INTO `noticias` (nome,slug,tags) VALUES ('$nome','$slug','$tags')");

            $insertNoticia->execute();
            $id = $conn->lastInsertId();
            if($insertNoticia){
                //preciso recuperar o id da minha notícia nessa url
                header("refresh: 3;painel.php?p=escrever-noticia&chave=".$_SESSION['id']."&autor=".$_SESSION['login']."&slug=".$slug."&id=".$id."");
                echo "<span class='msg sucesso'>Processando, aguarde...</span>";
            }else{
                echo "<span class='msg erro'>Erro ao postar essa notícia.</span>";
            }          
        }
    }
}

I removed the foreach, and added lastInsertId() to get the last id inserted in the database.

Browser other questions tagged

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