sequential code error

Asked

Viewed 313 times

0

I’m not getting my code working to create a sequential number can someone give me a hand? that’s the mistake:

Warning: mysqli_query() expects at least 2 parameters, 1 given in 
C:\Bitnami\wampstack-7.1.13-1\apache2\htdocs\mvpbx\php\caso.php on line 28

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null 
given in C:\Bitnami\wampstack-7.1.13-1\apache2\htdocs\mvpbx\php\caso.php on 
line 29
string(52) "INSERT INTO caso (numero, perfil_id) VALUES (1, '4')"

button to trigger the creation of the service number in the BD

  <form action="php/adiciona-caso.php"  method="post" >
 <li><button type="submit" value="4"
 name="perfil_id" id="perfil_id" class="btn btn-success btn-sm">Novo Atendimento</li></a>
 <input type="hidden" value="1" name="numero" id="numero">

adds case;

 <?php

$caso = new caso();
$caso->setNumero($_POST["numero"]);
$caso->setPerfil(new Perfil());
$caso->setPerfil($_POST["perfil_id"]);

//var_dump($caso);exit;

 $dao = new casoDAO($conexao);

 if ($dao->insereCaso($caso)) {   
   ?>
  <?php $_SESSION["atendimento_logado"] = $atendimento["numero"];?>
  <script type="text/javascript">
  window.location="../novo-atendimento.php"
      </script>
  <?php

DAO

function insereCaso($caso) {

                $query =  "INSERT INTO caso (numero, perfil_id) VALUES ($caso->getNumero()), '{$caso->getPerfil()}')";

 //var_dump($query);exit;
  $resultado = mysqli_query($conexao, $query);
  $caso = mysqli_fetch_assoc($resultado);
   return $caso;
     }

case class

class Caso
{

private $conexao;
function __construct($conexao){
    $this->conexao = $conexao;
}
public $id;
public $numero;
public $perfil;


 public function getNumero()
{
    return $this->numero;
}

public function setNumero($numero)
{
    $query = mysqli_query($conexao, "SELECT * FROM caso ORDER BY numero DESC LIMIT 1");
    $array = mysqli_fetch_array($query);
    $ultimo  = $array["numero"];
    $numero = $ultimo+1; // Peguei o ultimo numero e somei 1
    $this->numero = $numero;
}

1 answer

2

The mistake itself is telling you what’s wrong.

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: Bitnami wampstack-7.1.13-1 apache2 htdocs mvpbx php case.php on line 28

The second error is the consequence of the first error.

Try changing in class caso of

$query = mysqli_query("SELECT * FROM caso ORDER BY numero DESC LIMIT 1");

To:

$query = mysqli_query($conexao, "SELECT * FROM caso ORDER BY numero DESC LIMIT 1");

Note: Your code is not very clear, and I couldn’t identify where it’s coming from $conexao, then beware of injecting the connection into the methods that require it.

  • Thanks Vinicius, really in class if there was no connection then I created (changed in the post)

  • now the error is this: Fatal error: Uncaught Argumentcounterror: Too few Arguments to Function Case::__Construct(), 0 passed in C: Bitnami wampstack-7.1.13-1 apache2 htdocs mvpbx php adds-case.php on line 12 and Exactly 1 expected in C: Bitnami wampstack-7.1.13-1 apache2 htdocs mvpbx php case.php:7 Stack trace: #0 C: Bitnami wampstack-7.1.13-1 apache2 htdocs mvpbx php adds-case.php(12): Case->__Construct() #1 {main thrown in C: Bitnami wampstack-7.1.13-1 apache2 htdocs mvpbx php case.php on line 7

  • The mistake is telling you exactly that Fatal error: Uncaught ArgumentCountError: Too few arguments to function Caso::__construct(), 0 passed. In the archive adiciona caso you are instantiating the class $caso = new Caso(); when it should be $caso = new Caso($conexao);. Just read the error ;)

Browser other questions tagged

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