Error when registering product

Asked

Viewed 57 times

-2

Good afternoon guys, on my site has the products section

When I register a product it does not appear but when I register another it appears

Example> when I register the product Vinicius he doesn’t show up then I register another product called test, hence the product Vinicius begins to appear and the product test does not appear

        <div class="container">
            <div class="row">
                <?php
                        include_once("conect.php");
                        $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
                        $result_usuario = "SELECT * FROM produ_usu WHERE id = '$id'";
                        $resultado_usuario = mysqli_query($conn, $result_usuario);
                        $row_usuario = mysqli_fetch_assoc($resultado_usuario);


                        if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
                            ?>

                                    <?php
                                    while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
                                        ?>
                                        
                                        <div class="col-sm-4">
                                            <div class="block-q">
                                                <h3>Nome do Produto: <?php echo $row_usuario['produto2']; ?></h3>
                                                <hr>
                                                <p>Email do Cadastro: <?php echo $row_usuario['email']; ?></p>
                                                <p>ID do Cadastro: <?php echo $row_usuario['id_primary']; ?></p>
                                                <p>ID do Usuário: <?php echo $row_usuario['id']; ?></p>

                                                <a href="envia-orcamento.php?id_primary=<?php echo $row_usuario['id_primary']; ?>" class="btn btn-primary">Enviar Orçamento</a>
                                            </div>
                                        </div>

                                        <?php
                                    }?>
                        <?php
                        }else{
                            echo "<div class='alert alert-danger' role='alert'>Nenhum orçamento cadastrado!</div>";
                        }

                        ?>
            </div>
        </div>

this is my code showing the products registered by user


This is my code that registers the product in the database

<?php
        session_start();
        include_once("conexao.php");

        $nome2 = filter_input(INPUT_POST, 'nome2', FILTER_SANITIZE_STRING);
        $telefone2 = filter_input(INPUT_POST, 'telefone2', FILTER_SANITIZE_STRING);
        $produto2 = filter_input(INPUT_POST, 'produto2', FILTER_SANITIZE_STRING);
        $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING);
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);




        //echo "senha: $senha <br>";
        //echo "E-mail: $email <br>";

        $result_usuario = "INSERT INTO produ_usu (nome2, telefone2, produto2, id, email, created) VALUES ('$nome2', '$telefone2', '$produto2', '$id', '$email', NOW())";
        $resultado_usuario = mysqli_query($conn, $result_usuario);

        if(mysqli_insert_id($conn)){
            $_SESSION['msg'] = "<p style='color:green;'>Usuário cadastrado com sucesso</p>";
            header("Location: sucess.php");
        }else{
            $_SESSION['msg'] = "<p style='color:red;'>Usuário não foi cadastrado com sucesso</p>";
            header("Location: sucess.php");
        }

Thanks in advance!!!

  • Right, you register and then go to sucess.php. On the page that has the SELECT from which comes the variable $id that is in the clause WHERE id = '$id'"

  • Not the successful page is just a warning the error is happening on another page

  • I know this, the first sentence of my comment is a statement. The question is of the second sentence, that is, On the page that has the SELECT from which comes the variable $id that is in the clause WHERE id = '$id'"

  • Dude I don’t understand I’m new at this I’m 15 years old and I started programming, could you please explain to me

  • How do you access that first page of the question? which is the link?

  • http://localhost/ESQUINA-DAS-TINTAS/Adm/clients/client.php? id=20

  • I get it, let’s assume you’re registering Vinicius, then go to the page to check, is the Vinicius id 20? after the registration you saw there in the bank his id?

  • It’s not right the id is 20 even

  • with regard to this everything is ok

  • I believe the id on the select page is the previous id

  • I don’t think because he registers that id because then I register another product and the product that I registered in the past appears

  • And the new one doesn’t appear

  • I will create an environment here on my server

  • Blz, vlw mutio guy thanks really

  • who is this id_primary ?

  • This Primary id is the product id

  • and user ID and id

  • and how id_primary is inserted in the table

  • he is an INT a Primary key

  • id_primary = AUTO_INCREMENT

  • have you tried commenting this line $row_usuario = mysqli_fetch_assoc($resultado_usuario); just below $resultado_usuario

  • Do not add a comment to your question or an answer to say "Thank you". Comments are intended to ask for clarification, leave constructive criticism or add more relevant but brief information - not to socialize. If you wish to say "thank you", vote for or accept that person’s reply. See how https://i.stack.Imgur.com/evLUR.png

Show 17 more comments

2 answers

0


line to be withdrawn this line is used inside the while

$row_usuario = mysqli_fetch_assoc($resultado_usuario);

Code

  <div class="container">
     <div class="row">
        <?php
             include_once("conect.php");
             $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
             $result_usuario = "SELECT * FROM produ_usu WHERE id = '$id'";
             $resultado_usuario = mysqli_query($conn, $result_usuario);

             /*##### linha retirada ################################### */
               //$row_usuario = mysqli_fetch_assoc($resultado_usuario);
             /*####################################################### */

              if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
                   while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
         ?>
           <div class="col-sm-4">
            .....................
            .....................

mysqli uses a cursor to control the items you recover through the mysqli_fetch_assoc that advances 1 item each time it is called, and as you have already given one mysqli_fetch_assoc there in $row_usuario = mysqli_fetch_assoc($resultado_usuario); , your while is starting on the next item.

-2

$result_usuario = "SELECT * FROM produ_usu WHERE id = '$id'";

replaced by

$result_usuario = "SELECT * FROM produ_usu";
  • Now users have the same product

  • this cannot happen because each user has to have their product

  • The error is somewhere else so where this $id value comes from it’s getting the wrong value, try reading the documentation of the function https://www.php.net/manual/en/mysqli.insert-id.php mysqli_insert_id()

Browser other questions tagged

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