Wait for a parameter

Asked

Viewed 34 times

-2

I consulted in the forum a problem like mine, or even error code, but in my case I could not solve.

Notice: Undefined variable: link in C: xampp htdocs cadastro sacola.php on line 30 Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs cadastro sacola.php on line 30

php bag.

<?php session_start();
include_once 'includes/header.inc.php';
include_once 'includes/menu.inc.php';
?>

<!-- Formulário de Cadastro Cliente -->
        <div class="row container">
            <p>&nbsp;</p>
                <fieldset class="formulario" style="padding: 5px; width: 650px;">
                    <legend><img src="imagens/sacola.png" alt="[imagem]"style="width:50px; height:45px"></legend>
                    <br>

                    <?php
                        if(isset($_SESSION['msg'])):
                            echo $_SESSION['msg'];
                            session_unset();
                        endif;
                    ?>

                    <?php

                        if (!@include_once "conexao.php" ) {echo "Arquivo: conexao.php não incluso";};
                        $x = 1;
                        $sacola = 1;
                        while($x <= 4) {
                            echo "<div class='linha".$x."'>";
                            $y = 1;
                            while ($y <= 5) {
                                $sql = "SELECT SACOLA FROM TB_EMPRESTIMO WHERE SACOLA = ".$sacola." and SITUACAO = 2";
                                if($resultset = mysqli_fetch_assoc(mysqli_query($sql))) {;    
                                    echo "<button id='botao' data-numero='".$sacola."' onclick='mudaCor(this)' style='width:120px; height:37px; background-color: #FF0000;'>".$sacola."</button>";
                                } else {
                                    echo "<button id='botao' data-numero='".$sacola."' onclick='mudaCor(this)' style='width:120px; height:37px; background-color: #0000FF;'>".$sacola."</button>";                                                                     
                                }
                                $sacola = $sacola + 1;
                                $y = $y + 1;
                            }
                            $x = $x + 1;
                        }
                    ?>

                </fieldset>
        </div>


<?php include_once 'includes/footer.inc.php';?>
<?php include_once 'includes/script.inc.php';

create.php

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

$nome           = $_POST [ 'nome' ];
$telefone       = $_POST [ 'telefone' ];
$cpf            = $_POST [ 'cpf' ];
$sacola         = $_POST [ 'sacola' ];
$periodo        = $_POST [ 'periodo' ];
$situacao       = $_POST [ 'situacao' ];


$queryInsert = $link->query("insert into tb_emprestimo VALUES(NULL,'".$nome."','".$telefone."','".$cpf."','".$sacola."','".$periodo."','".$situacao."')");
   $affected_rows = mysqli_affected_rows($link);

    if($affected_rows > 0):
        $_SESSION['msg'] = "<p class='center green-text'>".'Empréstimo efetuado com Sucesso!'."<br>";
        header("Location: ../sacola.php");
endif;

php loan.

<?php session_start();
include_once 'includes/header.inc.php';
include_once 'includes/menu.inc.php';
?>

<!-- Formulário de Cadastro Cliente -->
        <div class="row container">
            <p>&nbsp;</p>
            <form action="banco_de_dados/create.php" method="post" class="col s12">
                <fieldset class="formulario" style="padding: 5px">
                    <legend><img src="imagens/sacola.png" alt="[imagem]"style="width:80px; height:45px"></legend>
                    <br>

                    <?php
                        if(isset($_SESSION['msg'])):
                            echo $_SESSION['msg'];
                            session_unset();
                        endif;
                    ?>

                    <!-- Campo Nome -->
                    <div class="input-field col s12">
                        <i class="material-icons prefix">account_circle</i>
                        <input type="text" name="nome" id="nome" maxlength="40" required autofocus>
                        <label for="nome">Nome</label>
                    </div>

                    <!-- Campo Telefone -->
                    <div class="input-field col s12">
                        <i class="material-icons prefix">phone</i>
                        <input type="text" name="telefone" id="telefone" maxlength="13" required>
                        <label for="telefone">Telefone</label>
                    </div>

                   <!-- Campo Cpf -->
                    <div class="input-field col s12">
                        <i class="material-icons prefix">credit_card</i>
                        <input type="text" name="cpf" id="cpf" maxlength="14" required>
                        <label for="cpf"> CPF</label>
                    </div>

                    <!-- Campo Número da Sacola -->
                    <div class="input-field col s12">
                        <i class="material-icons prefix">mode_edit</i>
                        <label>Numero da Sacola</label>
                        <input type="text" value="<?php echo isset($_GET['sacola']) ? $_GET['sacola'] : null; ?>">
                    </div>

                    <!-- Campo Periodo -->
                    <div class="input-field col s12">
                        <i class="material-icons prefix">timelapse</i>
                            <select name="periodo">
                                <option value="" disabled selected>Período</option>
                                <option value="manha">Manhã</option>
                                <option value="tarde">Tarde</option>
                                <option value="noite">Noite</option>
                            </select>
                    </div>

                    <!-- Botões -->
                    <div class="input-field col s12">
                        <input type="submit"style="width:120px; height:37px" value="Empréstimo" class="btn blue">
                        <input type="submit"style="width:120px; height:37px" value="Devolução" class="btn red">
                        <input type="reset" style="width:120px; height:37px" value="limpar" class="btn lilac">
                    </div>

                </fieldset>
            </form>
        </div> 

<?php include_once 'includes/script.inc.php';

Page script

    <!-- Arquivos Jquery e JavaScrip -->
        <script type="text/javascript" src="materialize/js/jquery-3.3.1.min.js"></script>
        <script type="text/javascript" src="materialize/js/materialize.min.js"></script>
        <script type="text/javascript" src="materialize/js/jquery.mask.min"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.3.1/jquery.maskedinput.min.js"></script>

        <!-- Inicialização Jquery -->
        <script type="text/javascript">
            $(document).ready(function(){
               $('.sidenav').sidenav();
               $('.dropdown-trigger').dropdown();
            });   
        </script>

        <!-- Inicialização Jquery Mascaras -->
        <script type="text/javascript"> 
            jQuery.noConflict();
            jQuery(function($){
               //$("#datanasc").mask("99/99/9999");
               //$("#telefone").mask("(099)99999-9999");
               $("#cpf").mask("999.999.999-99"); 
            });


          document.addEventListener('DOMContentLoaded', function() {
            var elems = document.querySelectorAll('select');
            var instances = M.FormSelect.init(elems, options);
            });

            // Or with jQuery

            $(document).ready(function(){
              $('select').formSelect();
            });
        </script>

        <script>
            //pega todos os botões
            const botões = document.querySelectorAll('#botao');

            for (let i = 0; i < botões.length; i++) {
                //escuta o evento de click em cada botao
                botões[i].addEventListener('click', function (e) {
                    //pega o numero do botao clicado
                    const sacola = this.dataset.numero;

                    //redireciona para emprestimos.php passando o numero do botao
                    window.location.href = `emprestimo.php?sacola=${sacola}`;
                });
            }
        </script>


        <script>
            //muda cor do botão
            function mudaCor(el){
                var cor = el.style.backgroundColor;
                    if(cor == 'rgb(0, 0, 255)') {
                    el.style.backgroundColor = '#FF0000';
                    } else {
                    el.style.backgroundColor = '#0000FF';
                    }
            }
        </script>       
    </body>
</html>

Phpadmin database structure and error screen image:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Connexion.php

<?php $link = new mysqli('localhost','root','','db_sacola'); $link->set_charset('utf8'); if(!$link){ echo "Problemas ao realizar a conexao com o banco de dados (".mysqli_error().")"; exit(); } –

1 answer

0

The function mysqli_query requires 2 parameters, the first being the connection and the second the query. mysqli_query($con,$sql). find the variable of your connection and put it in the function as well as your query

  • My connection is like this!

  • I edited and posted my connection.php, I don’t know if I could do so it makes the right connection, however I tried to use the variable in the function as said, but it did not work

  • you called the $link answer inside the mysqli_query?

  • I haven’t been able to

  • I put the variable $link before, now gives this error!

  • Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs cadastro sacola.php on line 30

Show 1 more comment

Browser other questions tagged

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